js中的自定义异常处理函数
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
- Use the select box to set or change the error handler.
- 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中的自定义异常处理函数的更多相关文章
- JS中的自执行函数
本来规划的是2013年,狠狠的将JS学习下,谁知计划赶不上变化,计划泡汤了.13年的我对JS来说可以说是属于跟风,对它的理解和认识也仅仅是皮毛而已,也是因为要完成<ArcGIS API for ...
- main.js中封装全局登录函数
1. 在 main.js 中封装全局登录函数 通过 vue 对象的原型扩展,可以扩展一个函数,这样这个函数就可以在每一个界面通过类似指向对象的方式,去访问这个函数. 如下是 main.js 扩展的函数 ...
- js中的三种函数写法
js中的三种函数写法 <script type="text/javascript"> //普通的声明方式 function myFun(m,n){ alert(m+n) ...
- JS中构造函数和普通函数有什么区别
JS中构造函数有普通函数有什么区别? 1.一般规则 构造函数都应该以 一个大写字母开头,eg: function Person(){...} 而非构造函数则应该以一个小写字母开头,eg: functi ...
- asp.net类似于js中的setTimeOut()的函数作用?
asp.net类似于js中的setTimeOut()的函数作用? 插入这行即可,定时2秒,再运行下一步: System.Threading.Thread.Sleep(); 加个随机数 Random r ...
- 详解如何在Laravel中增加自定义全局函数
http://www.php.cn/php-weizijiaocheng-383928.html 如何在Laravel中增加自定义全局函数?在我们的应用里经常会有一些全局都可能会用的函数,我们应该怎么 ...
- JS中的高阶函数
JS中的高阶函数 高阶函数是指以函数作为参数的函数,并且可以将函数作为结果返回的函数. 1. 高阶函数 接受一个或多个函数作为输入 输出一个函数 至少满足以上一个条件的函数 在js的内置对象中同样存在 ...
- 【转载】JS中bind方法与函数柯里化
原生bind方法 不同于jQuery中的bind方法只是简单的绑定事件函数,原生js中bind()方法略复杂,该方法上在ES5中被引入,大概就是IE9+等现代浏览器都支持了(有关ES5各项特性的支持情 ...
- JS中的几种函数
函数可以说是js中最具特色的地方,在这里我将分享一下有关函数的相关知识: 包装函数: (function foo(){...})作为函数表达式意味着foo只能在...所代表的位置中被访问 ...
随机推荐
- dumpkeys - 转储显示键盘翻译表
总览 (SYNOPSIS) dumpkeys [ -hilfn1 -Sshape -ccharset --help --short-info --long-info --numeric --full- ...
- 1-1 编程基础 GCC程序编译
GCC简介 Linux系统下的gcc是GNU推出的强大.性能优越的多平台编译器,是GNU的代表作之一.gcc可以在多种硬体平台上编译出可执行程序,其执行效率与一般的编译器相比平局效率要高20 ...
- 关于sigleton模式
单例模式的要点有三个:一是某个类只能有一个实例:二是它必须自行创建这个实例:三是它必须自行向整个系统提供这个实例. 从具体实现角度来说,就是以下三点:一是单例模式的类只提供私有的构造函数,二是类定义中 ...
- Redux 和 mobx的区别
Redux: Redux将数据保存在单一store中,Mobx将数据保存在分散的多个store中 Redux需要手动处理变化后的操作,Mobx使用observable保存数据,数据变化后自动处理响应的 ...
- QT+创建两个不相干的窗口实现一个显示一个不显示
因为两个窗口互不相干,所以需要重新创建一个窗口类subWidget subWidget.cpp文件 #ifndef SUBWIDGET_H #define SUBWIDGET_H #include & ...
- int (*a)[10]和int *a[10]的区别
有点晚了,放个链接,睡觉. https://stackoverflow.com/questions/13910749/difference-between-ptr10-and-ptr10
- (9) openssl enc(对称加密)
对称加密工具,了解对称加密的原理后就很简单了,原理部分见下文. openssl enc -ciphername [-in filename] [-out filename] [-pa ...
- 高可用技术之keepalived原理简单了解
Keepalived 工作原理 keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议. 虚拟路由冗余协 ...
- 分分钟钟学会Python - 函数(function)
函数(function) 1 基本结构 本质:将多行代码拿到别处并起个名字,以后通过名字就可以找到这行代码并执行 应用场景: 代码重复执行 代码量很多超过一屏,可以选择通过函数进行代码的分割 写代码方 ...
- tiles介绍
主要目的是为了将复数的jsp页面作为一个的页面的部分机能,然后用来组合成一个最终表示用页面用的,这样的话,便于对页面的各个机能的变更及维护. Tiles使得struts在页面的处理方面多了一种选择.并 ...