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只能在...所代表的位置中被访问 ...
随机推荐
- uva12433 Rent a Car
init 一开始搞成2*n+2了...囧 所以初始化很重要! 然后提交的时候忘了删调试的数据了..囧 技巧:设立虚拟节点 建图比较麻烦(非常). 要考虑到保养完了的车可以免费再用 设立S,T ,1 ...
- 用户交互和if条件判断、嵌套
#a=input("提示语“)#接受的数据类型是字符串str#提示用户输入姓名 # a=input("请输入姓名") print(a) '''输出结果:请输入姓名小明 姓 ...
- QT+信号和槽函数_自定义槽函数_一个信号对应多个槽函数
以下的代码里面有自定义槽函数的内容,同时也有信号实现的函数: #ifndef MAINWIDGET_H #define MAINWIDGET_H #include <QWidget> #i ...
- 《3+1团队》【Alpha】Scrum meeting 4
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...
- 717. 1-bit and 2-bit Characters@python
We have two special characters. The first character can be represented by one bit 0. The second char ...
- noip2019——动态规划刷题历程
加粗的是值得总结的 从洛谷的普及题开始刷题: 背包式dp(有些技巧的) 1.p2639[USACO09OCT]Bessie的体重问题 -p1049取模意义下01背包 技巧:重量=价值 2.金明的预算问 ...
- 关于react-native引入第三方库,运行react-native link xxxxx后项目无法运行
最新版本的react-native中,官方表示将要移除 AsyncStorage,推荐使用@react-native-community/async-storage,好吧官方经常这么干 按照官方提 ...
- uC/OSii之任务划分
满足以下几个指标将会使软件设计比较简洁高效. 满足实时性指标 任务数目合理 简化软件系统 降低资源需求 设备依赖性划分:主要是对于一些输入输出设备进行划分,输入输出设备分为主动型和被动性.主动型有一个 ...
- PyMySQL操作mysql数据库(py3必学)
一,安装PyMySQL Python是编程语言,MySQL是数据库,它们是两种不同的技术:要想使Python操作MySQL数据库需要使用驱动.这里选用PyMySQL驱动. 安装方式还是使用pip命令. ...
- 大数据学习——hive基本操作
1 建表 create table student(id int,name string ,age int) row format delimitedfields terminated by ','; ...