简体   繁体   English

如何获取在javascript中加载的javascript文件名?

[英]How to get the file name of javascript loaded in javascript?

Suppose I have a HTML file. 假设我有一个HTML文件。 In that file, I am referencing two external javascript files. 在该文件中,我引用了两个外部javascript文件。 Among those, one javascript file has error in it (mostly compilation errors). 其中,一个javascript文件有错误(主要是编译错误)。 And I have written onerror method in another javascript file which does not have any error. 而且我在另一个没有任何错误的javascript文件中编写了onerror方法。 Now when I open the HTML file in browser, obviously onerror will be called. 现在,当我在浏览器中打开HTML文件时,显然将调用onerror The contents in that method will be executed. 该方法中的内容将被执行。 Here my actual thought, I would like to print javascript file name which has error in it in an alert(-) placed inside onerror method of another javascript file. 在这里,我的实际想法是,我想在另一个JavaScript文件的onerror方法中放置的alert(-)打印其中有错误的javascript文件名。

If I put arguments.callee statement inside alert(-) , then it is printing current onerror method content. 如果我将arguments.callee语句放在alert(-) ,则它正在打印当前的onerror方法内容。 If i put arguments.callee.caller , then it is printing null. 如果我把arguments.callee.caller放进去,那就打印null了。

Is there any way to find file name of javascript loaded which has errors in it through programatically, specially in javascript? 有什么方法可以通过编程方式(特别是在javascript中)查找已加载的javascript的文件名,该文件中存在错误?

This question is pretty well answered in https://developer.mozilla.org/de/docs/Web/API/GlobalEventHandlers/onerror . 这个问题在https://developer.mozilla.org/de/docs/Web/API/GlobalEventHandlers/onerror中得到了很好的回答。 Especially look at this: 特别是看一下:

funcRef is a reference to a function. funcRef是对函数的引用。 When the function returns true, this prevents the firing of the default event handler. 当函数返回true时,这将防止触发默认事件处理程序。 Function parameters: 功能参数:

 Error message (string) URL of the script where the error was raised (string) Line number where error was raised (number) Column number for the line where the error occurred (number) Error Object (object) 

This results in: 结果是:

window.onerror(msg, URL, line, col, e) {

       alert("Error in " + URL + " at " + line + "/" + col);

}

I recommend to not use alert for showing script errors but the console object. 我建议不要使用警报来显示脚本错误,而应使用console对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM