1. Can I suppress JavaScript error messages?

2. Can I set up my own JavaScript error handler?

3. Can I change the active JavaScript error handler? (Demo)

Question: Can I suppress JavaScript error messages?

Answer: Yes – although usually it is not a good design decision to do so. (Normally, you are much better off fixing the errors, rather than just suppressing the error messages.)

To suppress all JavaScript error messages on your HTML page, you can put the following script code in the <HEAD> section of your page:

<SCRIPT language="JavaScript">
<!--
function silentErrorHandler() {return true;}
window.onerror=silentErrorHandler;
//-->
</SCRIPT>

Or you can use a similar code fragment (without the SCRIPT tags) in an included .js file, if you have one. For a working code example, see this error handling demo!

Question: Can I set up my own JavaScript error handler?

Answer: Yes. To define your own error handler, use this JavaScript code:

function handlerFunction(description,page,line) {
// put error-handling operators here
return true;
}
window.onerror=handlerFunction;

Your error handler function can optionally use the following parameters:

  • a textual description of the error
  • the address (URL) of the page on which the error occurred
  • the number of the line in which the error occurred

The error handler function must return false if you wish to invoke the browser's default error handler after your own handler finishes. If you don't want to invoke the browser's default handler, your handler function must return true. For an additional code example, check out this error handling demo!

Question: Can I dynamically change the JavaScript error handler?

Answer: Yes. To change the JavaScript error handler, just setwindow.onerror to the name of the function that will serve as your new error handler.

Here's a demo that lets you test three different error handlers:

  • the browser's default error handler
  • an error handler that displays a customized alert box
  • a "silent" error handler that suppresses all error messages. 
    Custom Error Handler 
    Silent Error Handler 
    Default Error Handler 
     
    1. Use the select box to set or change the error handler.
    2. Click Fire an Error to test the active error handler.

    Below is the source code of the error handling functions used in this demo:

    function defaultHandler() {return false}
    function silentHandler() {return true}
    function customHandler(desc,page,line,chr) {
    alert(
    'JavaScript error occurred! \n'
    +'The error was handled by '
    +'a customized error handler.\n'
    +'\nError description: \t'+desc
    +'\nPage address: \t'+page
    +'\nLine number: \t'+line
    )
    return true
    }

js中的自定义异常处理函数的更多相关文章

  1. JS中的自执行函数

    本来规划的是2013年,狠狠的将JS学习下,谁知计划赶不上变化,计划泡汤了.13年的我对JS来说可以说是属于跟风,对它的理解和认识也仅仅是皮毛而已,也是因为要完成<ArcGIS API for ...

  2. main.js中封装全局登录函数

    1. 在 main.js 中封装全局登录函数 通过 vue 对象的原型扩展,可以扩展一个函数,这样这个函数就可以在每一个界面通过类似指向对象的方式,去访问这个函数. 如下是 main.js 扩展的函数 ...

  3. js中的三种函数写法

    js中的三种函数写法 <script type="text/javascript"> //普通的声明方式 function myFun(m,n){ alert(m+n) ...

  4. JS中构造函数和普通函数有什么区别

    JS中构造函数有普通函数有什么区别? 1.一般规则 构造函数都应该以 一个大写字母开头,eg: function Person(){...} 而非构造函数则应该以一个小写字母开头,eg: functi ...

  5. asp.net类似于js中的setTimeOut()的函数作用?

    asp.net类似于js中的setTimeOut()的函数作用? 插入这行即可,定时2秒,再运行下一步: System.Threading.Thread.Sleep(); 加个随机数 Random r ...

  6. 详解如何在Laravel中增加自定义全局函数

    http://www.php.cn/php-weizijiaocheng-383928.html 如何在Laravel中增加自定义全局函数?在我们的应用里经常会有一些全局都可能会用的函数,我们应该怎么 ...

  7. JS中的高阶函数

    JS中的高阶函数 高阶函数是指以函数作为参数的函数,并且可以将函数作为结果返回的函数. 1. 高阶函数 接受一个或多个函数作为输入 输出一个函数 至少满足以上一个条件的函数 在js的内置对象中同样存在 ...

  8. 【转载】JS中bind方法与函数柯里化

    原生bind方法 不同于jQuery中的bind方法只是简单的绑定事件函数,原生js中bind()方法略复杂,该方法上在ES5中被引入,大概就是IE9+等现代浏览器都支持了(有关ES5各项特性的支持情 ...

  9. JS中的几种函数

    函数可以说是js中最具特色的地方,在这里我将分享一下有关函数的相关知识: 包装函数:        (function foo(){...})作为函数表达式意味着foo只能在...所代表的位置中被访问 ...

随机推荐

  1. Asp.Net Core 入门(三) —— 自定义中间件

    上一篇我们讲了Startup文件,其中着重介绍了中间件,现在我们就来自定义我们自己的中间件吧. 中间件通常封装在一个类中,并使用扩展方法进行暴露.它需要拥有一个类型为RequestDelegate的成 ...

  2. Windows SubSystem for Linux(WSL)设置默认和设置默认登陆用户

    使用wslconfig命令进行管理 1.  设置默认运行的linux系统 wslconfig /setdefault <DistributionName> 正如上面所说,如果执行wslco ...

  3. python爬虫---从零开始(三)Requests库

    1,什么是Requests库 Requests是用python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库. 它比urllib更加方便,可以节约我们大量的工作 ...

  4. Java笔记:编写第一个Java程序

    2017.6.17 1.编写第一个Java程序 创建text文本,命名第一个Java程序.txt 在里面编写Java代码 public class Demo1{ public static void ...

  5. jquery实现密码强度检测

    jquery实现密码强度验证   jquery实现密码强度验证 JS代码:   $('#pass').keyup(function(e) { var strongRegex = new RegExp( ...

  6. 环境变量HISTCONTROL命令及对快捷键Ctrl+o命令的影响

    在linux中环境变量HISTCONTROL可以控制历史的记录方式. HISTCONTROL有以下的选项: ignoredups          默认,忽略重复命令 ignorespace      ...

  7. vue-loader 细节

    vue-loader 能根据 .vue 文件,导入一个vue组件.我这里从 vue-cli 的构建项目中抽取了vue-loader 一个小例子出来:vuedemo/demo02 vue-loader ...

  8. 条款9:绝不在构造和析构过程中调用virtual函数(Never call virtual functions during construction or destruction)

    NOTE:在构造和析构期间不要调用virtual函数,因为这类调用从不下降至derived class(比起当前执行构造函数和析构函数的那层)

  9. python中的多任务

    多任务 什么是任务 一个电脑运行这的软件 什么是多任务 电脑同时运行着的多个软件 多任务原理 时间片的轮转 并行与并发 并发:假的多任务,多个任务共用一个核 并行:正的多任务,一个核处理一个程序 生理 ...

  10. Crossed Ladders 求街道宽度 (二分法)

    Description A narrow street is lined with tall buildings. An x foot long ladder is rested at the bas ...