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. Python3简明教程(四)—— 流程控制之分支

    我们通过 if-else 语句来做决定,来改变程序运行的流程. if语句 语法如下: if expression: do this 如果表达式 expression 的值为真(不为零的任何值都为真), ...

  2. c++调用com口操作autocad

    #include "stdafx.h" #include <atlcomcli.h> #import "D:\\C++test\\FirstCom\\Rele ...

  3. OpenCV2:第五章 访问图像

    一.行/列访问 1.单行/单列访问 Mat Mat::row(int i) const Mat Mat::col(int j) const 2.多行/多列访问 Range(start,end); Ra ...

  4. 【lua实战摸索】在b.lua调用a.lua的函数

    需要掌握知识: lua table的使用(创建自己函数的表作为函数库) 普通函数的调用:tab.func(tab,参数) 等效于表中函数的调用tab:func(参数) 基本思路: 1.在相同目录下创建 ...

  5. 洛谷——P3801 红色的幻想乡

    P3801 红色的幻想乡 推荐阅读 https://blog.csdn.net/qq_41252892/article/details/79035942 非常清楚 线段树单点修改 emmm没什么了 # ...

  6. POI导出,开发中经常会遇到数据导出这样的问题,下面是我在开发中采用的解决方法,大家可以参考,具体的实现害的结合你自身的业务逻辑

    @RequestMapping(value = "/drawPayFailExport",method = RequestMethod.GET,produces = "a ...

  7. elementary 5.0 安装 chrome

    sudo apt install google-chrome-stable wget -q -O - https://dl.google.com/linux/linux_signing_key.pub ...

  8. 前端基础之JavaScript_1

    摘要: JavaScript简介 引入方式 语言规范 JavaScript语言基础 变量声明 数据类型 运算符 流程控制 函数 词法分析 内置对象 一.JavaScript概述 1.ECMAScrip ...

  9. 关于在views对models创建的表的简单操作

    models.User.objects.create(c1='a',c2='b') obj=models.User(c1='a',c2='b') obj.save() 增加 models.User.o ...

  10. 【02】SASS与SCSS

    SASS语法 SASS语法也称之为SASS的缩进语法,其目的是担供一个更简洁的语法.对于一些人来说,更多的是基于于CSS的美学吸引力,用SASS来代替SCSS语法. SASS语法和CSS语法不一样,他 ...