简体   繁体   English

相同的JavaScript代码在两种不同的环境中的行为不同

[英]Same JavaScript code behaves differently in two different environments

I had a working jQuery Progress Bar code ( http://jqueryui.com/progressbar/ ) in a huge web site until a new release crashed it with error "Uncaught TypeError: Object [object Object] has no method 'progressbar'". 在一个巨大的网站上,我有一个工作正常的jQuery Progress Bar Code( http://jqueryui.com/progressbar/ ),直到新版本将其崩溃并显示错误“ Uncaught TypeError:Object [object Object]没有方法'progressbar'”。

I identified the root cause as jQuery being added more than once which somehow happened during the release process. 我确定了根本原因是jQuery被多次添加,这在发布过程中以某种方式发生了。 Many other places on the website, for example, where jQuery Chosen ( http://plugins.jquery.com/chosen/ ) was used, crashed as well. 网站上的许多其他地方也崩溃了,例如,使用jQuery Chosen( http://plugins.jquery.com/chosen/ )。

I managed to fix it using jQuery.noConflict(); 我设法使用jQuery.noConflict()修复了它; before the problematics calls and everything went back to peace. 在出现问题电话之前,一切都恢复了和平。 New release to the production environment and everybody's happy. 新产品发布到生产环境中,大家都很高兴。

Turns out though that everything else but the jQuery Progress Bar is fixed. 事实证明,除了jQuery Progress Bar之外的所有其他东西都是固定的。 To make it even more fun, I went back in my testing environment and absolutely everything is working there, including the Progress bar. 为了使它变得更加有趣,我回到了测试环境中,并且绝对所有的东西都在那里工作,包括进度栏。

I compared all the files on the server with those in my test environment and the code is exactly the same. 我将服务器上的所有文件与测试环境中的文件进行了比较,并且代码完全相同。 All other JavaScript works with no problems on the server but the jQuery Progress Bar would only continue to work in my test environment but not in Production where the "Uncaught TypeError: Object [object Object] has no method 'progressbar'" persists. 所有其他JavaScript都可以在服务器上正常工作,但是jQuery Progress Bar只能在我的测试环境中继续工作,而不能在生产环境中继续工作,在生产环境中,“未捕获的TypeError:对象[object Object]没有方法'progressbar'”仍然存在。 Any ideas or hints what could be the issue? 任何想法或暗示可能是什么问题?

This type of errors occur not because conflicts, but because by the time your html loads, jquery starts executing., and find null reference to which you are trying the ui . 发生这种类型的错误的原因不是因为冲突,而是因为在加载html时,jquery开始执行。并找到尝试使用ui null引用。 The best way to do is, put the <script> tags at the end of the document. 最好的方法是将<script>标记放在文档的末尾。 Then, probably, your html would have been loaded, and the object to which you attached the progressbar will be ready to be attached to. 然后,可能已经加载了html,并且将您附加progressbarobject准备好附加。

And, Since our test environments are local, script files will be local and load instantly which is not the case with production and you can not be sure of different people's bandwidths. 而且,由于我们的测试环境是本地的,因此脚本文件将是本地的,并且会立即加载,这在生产环境中并非如此,您无法确定其他人的带宽。 And, these things sometimes work to fool us as they do with cached files, for the second time onward, but may not work for every client as well. 而且,这些事情有时会像欺骗缓存文件一样愚弄我们,这是第二次以后,但可能并非对每个客户端也一样。

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

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