我有一个脚本,在我的网站上检测Javascript错误,并将它们发送到我的后端报告。它报告遇到的第一个错误、假定的行号和时间。
编辑包含doctype:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" xmlns:fb="http://www.facebook.com/2008/fbml">
...
<script type="text/javascript">
//<![CDATA[
// for debugging javascript!
(function(window){
window.onerror = function(msg, url, ln) {
//transform errors
if (typeof(msg) === 'object' && msg.srcElement && msg.target) {
if(msg.srcElement == '[object HTMLScriptElement]' && msg.target == '[object HTMLScriptElement]'){
msg = 'Error loading script';
}else{
msg = 'Event Error - target:' + msg.target + ' srcElement:' + msg.srcElement;
}
}
msg = msg.toString();
//ignore errors
if(msg.indexOf("Location.toString") > -1){
return;
}
if(msg.indexOf("Error loading script") > -1){
return;
}
//report errors
window.onerror = function(){};
(new Image()).src = "/jserror.php?msg=" + encodeURIComponent(msg) + "&url=" + encodeURIComponent(url || document.location.toString().replace(/#.*$/, "")) + "&ln=" + parseInt(ln || 0) + "&r=" + (+new Date());
};
})(window);
//]]>
</script>
因为这个脚本,我敏锐地意识到在我的网站上发生的任何javascript错误。最大的错误之一是第0行上的“脚本错误”。Chrome 10+和Firefox 3+。这个错误在Internet Explorer中不存在(或者可能被称为其他错误?)
更正(2013年5月23日):这个“脚本错误,第0行”错误现在出现在IE7和其他版本的IE中。可能是最近IE安全补丁的结果,因为这种行为以前不存在。
有人知道这个错误是什么意思或者是什么原因造成的吗?它发生在我整个页面负载的0.25%左右,占报告错误的一半。
对于那些将来会遇到这个问题的人,有一个更新:
布鲁法的答案是正确的,而且没有变通办法。
显然,其他一些人也遇到了这个限制,Firefox的Bug 69301和WebKit的Bug 70574都需要修复
好消息是,随着Firefox 13的发布,这个bug已经得到解决。
下面是它的用法:
<script src="http://somremotesite.example/script.js" crossorigin>
crossorigin等价于crossorigin=anonymous,并告诉浏览器在不发送凭据的情况下对脚本执行CORS取回。
您必须确保脚本发送时带有一个与请求域匹配的Access-Control-Allow-Origin HTTP头值,例如:
Access-Control-Allow-Origin: http://myhomesite.example
Access-Control-Allow-Origin: *
否则浏览器将取消加载脚本。
Apache:
Header set Access-Control-Allow-Origin "*"
(其他web服务器请参见CORS示例。)
如果你用PHP发送脚本:
header('Access-Control-Allow-Origin', 'http://myhomesite.example');
我已经测试过了,它像预期的那样工作。来自script.js的所有错误都会被窗口捕获。带有消息、文件和行详细信息的Onerror处理程序。
WebKit漏洞尚未修复,但已经提出了一个补丁(并使用相同的解决方案)。希望修复会很快发布。
更多关于CORS的信息,请访问:http://enable-cors.org/
下面的怎么样?脚本错误是不可用的JavaScript,所以只是隔离的特定情况下,并处理它,尽你所能。
window.onerror = function (msg, url, lineNo, columnNo, error) {
var string = msg.toLowerCase();
var substring = "script error";
if (string.indexOf(substring) > -1){
alert('Script Error: See Browser Console for Detail');
} else {
alert(msg, url, lineNo, columnNo, error);
}
return false;
};
“脚本错误”发生在Firefox, Safari和Chrome中,当异常违反浏览器的同源策略时——即当错误发生在托管在当前页面域以外的域上的脚本中时。
This behavior is intentional, to prevent scripts from leaking information to external domains. For an example of why this is necessary, imagine accidentally visiting evilsite.com, that serves up a page with <script src="yourbank.com/index.html">. (yes, we're pointing that script tag at html, not JS). This will result in a script error, but the error is interesting because it can tell us if you're logged in or not. If you're logged in, the error might be 'Welcome Fred...' is undefined, whereas if you're not it might be 'Please Login ...' is undefined. Something along those lines.
如果evilsite.com为前20名左右的银行机构提供这种服务,他们就会非常清楚你访问了哪些银行网站,并可以提供更有针对性的钓鱼页面。(当然,这只是一个例子。但它说明了为什么浏览器不应该允许任何数据跨域边界。)
我已经在最新版本的Safari、Chrome和Firefox中测试了这个功能——它们都可以这样做。IE9没有——它对x原点异常和同源异常一视同仁。(Opera不支持onerror。)
来自马的口:WebKit源代码,在向onerror()传递异常时检查起源。以及检查的Firefox源代码。
更新(10/21/11):跟踪此问题的Firefox漏洞包含一个指向引发此行为的博客文章的链接。
更新(12/2/14):您现在可以通过在脚本标记上指定crossorigin属性并让服务器发送适当的CORS HTTP响应标头,在某些浏览器上启用完整的跨域错误报告。