jQuery的getScript问题 [英] jQuery getScript issue
问题描述
我的代码(用于测试目的):
jQuery.getScript(res / fader.js,function(){alert(loaded);});在Chrome(16)中,
完美无缺。在Firefox(11)中,萤幕控制台显示要求的脚本和预期的响应(包括应用程序/ javascript的mimetype)。但没有警觉。没有错误。没有。
如果我随后尝试引用应该已经加载的脚本中的某些内容,它仍然是未定义的(这是onload事件和控制台之后的几秒钟显示脚本已经被检索到)。
$ b $ p更新
我正在使用jQuery 1.9.1
感谢并未收集(添加答案并将其标记为已接受)我发现这个问题...
一个异常正在发生 - 这是由jQuery静静地处理的。通过getScript()添加处理程序显示问题是无效的分配左侧。不幸的是,这个异常并没有说错误发生在哪里。然后我尝试了一种加载脚本的不同方法(创建脚本元素并将其附加到主体),这意味着浏览器中的默认处理程序被触发了,这给了我行号和有问题的代码:
this = null;
这是为了清理对象实例,以避免内存泄漏并在Chrome中运行不在Firefox中。
While it seems like lots of people seem to hav been having trouble with this on Stack Overflow, I've yet see a description of why it was going wrong (1, 2, 3)
My code (for testing purposes):
jQuery.getScript("res/fader.js", function () { alert("loaded"); });
works flawlessly in Chrome (16). In Firefox (11) the firebug console shows the script being requested and the expected response (including the mimetype of application/javascript). But no alert. No error. Nothing.
If I subsequently try to reference something in the script which should have been loaded, it is still undefined (this is several seconds after the onload event and after the console shows the script has been retrieved).
update
I'm using jQuery 1.9.1
Kudos and thanks to uncollected (add an answer & I'll mark it as accepted) adding the exception hander, I found the problem...
An exception was occurring - which was being handled silently by jQuery. Adding the handler via getScript() revealed the problem was "invalid assignment left-hand side". Unfortunately this exception doesn't say where the error occurred. I then tried a different method of loading the script (creating a script element and appending it to the body) which meant that the default handler in the browser was triggered which gave me the line number and the bit of offending code:
this=null;
This is intended to clean up the object instance, to avoid a memory leak and works in Chrome, but not in Firefox.
这篇关于jQuery的getScript问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!