好的,事实证明$.getScript() 函数的默认实现会根据引用的脚本文件是否在同一个域中而有所不同。外部参考,例如:
$.getScript("http://www.someothersite.com/script.js")
会导致jQuery创建一个外部脚本引用,可以毫无问题地调试。
<script type="text/javascript" src="http://www.someothersite.com/script.js"></script>
但是,如果您引用本地脚本文件,例如以下任何内容:
$.getScript("http://www.mysite.com/script.js")
$.getScript("script.js")
$.getScript("/Scripts/script.js");
然后jQuery将异步下载脚本内容,然后将其添加为内联内容:
<script type="text/javascript">{your script here}</script>
后一种方法不适用于我测试过的任何调试器(Visual Studio.net、Firebug、IE8 调试器)。
解决方法是覆盖$.getScript() 函数,以便它始终创建外部引用而不是内联内容。这是执行此操作的脚本。我已经在 Firefox、Opera、Safari 和 IE 8 中对此进行了测试。
<script type="text/javascript">
// Replace the normal jQuery getScript function with one that supports
// debugging and which references the script files as external resources
// rather than inline.
jQuery.extend({
getScript: function(url, callback) {
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = url;
// Handle Script loading
{
var done = false;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function(){
if ( !done && (!this.readyState ||
this.readyState == "loaded" || this.readyState == "complete") ) {
done = true;
if (callback)
callback();
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
}
};
}
head.appendChild(script);
// We handle everything using the script element injection
return undefined;
},
});
</script>