两次加载 jquery 会导致错误吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2731897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
loading jquery twice causes an error?
提问by yuri
I have a page where jquery + other js's is being loaded:
我有一个正在加载 jquery + 其他 js 的页面:
<script src="/eiv/javascripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/eiv/javascripts/jquery.jeditable.js" type="text/javascript"></script>
<script src="/eiv/javascripts/jquery-ui-1.7.1.custom.min.js" type="text/javascript"></script>
<script src="/eiv/javascripts/corner.js" type="text/javascript"></script>
<script src="/eiv/javascripts/jquery.form.js" type="text/javascript"></script>
<script src="/eiv/javascripts/validationdate.js" type="text/javascript"></script>
I am loading tabs as follows:
我正在加载标签如下:
<%if (tabnum == 1) {%>
<script type="text/javascript">
$(document).ready(function(){
$("#tabs").tabs();
$("#tabs ul li a").corner('7px top');
var $tabs = $('#tabs').tabs();
$tabs.tabs('select', 0);
dateValidation();
changeOption();
deleteConfirmation();
});
</script>
<%} else if (tabnum==2) {%>
<script type="text/javascript">
$(document).ready(function(){
$("#tabs").tabs();
$("#tabs ul li a").corner('7px top');
var $tabs = $('#tabs').tabs();
$tabs.tabs('select', 1);
changeOption();
deleteConfirmation();
});
</script>
<%}%>
the validationdate.js
is a mine built js that checks dates and other stuff. it has this as the first line:
这validationdate.js
是一个我构建的 js,用于检查日期和其他内容。它有这个作为第一行:
document.write('<script type="text/javascript" src=""jquery-1.3.2.min.js""></script>');
Problem:Is that in production at time ..this page errors out and give a JS error. This causes the tab's to be not clickable. This error happens intermittently and I can not seem to reproduce it on my machine. Both machines are using IE. Error also happens in Firefox, though little JS error thing does not show up in firefox. and I even have firebug which does not show any JS error either.
问题:当时是否在生产中..此页面出错并给出 JS 错误。这会导致选项卡不可点击。此错误间歇性发生,我似乎无法在我的机器上重现它。两台机器都使用IE。Firefox 中也会发生错误,尽管在 Firefox 中没有出现一些 JS 错误。我什至有萤火虫,它也没有显示任何 JS 错误。
I suspect that error is coming up because validationdate.js
is also loading jquery-1.3.2.min.js
. Can this be an error?
我怀疑会出现错误,因为validationdate.js
也在加载jquery-1.3.2.min.js
. 这可能是一个错误吗?
By the way, the JS error I get is 'Exception caught but not thrown ..line 23' and line 23 is following
顺便说一句,我得到的 JS 错误是“异常被捕获但未抛出 ..第 23 行”,第 23 行如下
<script src="/eiv/javascripts/jquery-1.3.2.min.js" type="text/javascript"></script>
I am really out of options here and am willing to try stuff out. and also ways to reproduce on my machine so i can fix it!!
我在这里真的没有选择,并且愿意尝试一些东西。以及在我的机器上复制的方法,以便我可以修复它!!
回答by Nick Craver
First to to answer your question, yes, including jQuery twice can cause all sorts of issues.
首先回答您的问题,是的,两次包含 jQuery 会导致各种问题。
To fix it, this line:
要修复它,这一行:
document.write('<script type="text/javascript" src=""jquery-1.3.2.min.js""></script>');
Should be wrapped to check if jQuery already exists instead of blindly including it again, like this:
应该包装以检查 jQuery 是否已经存在,而不是盲目地再次包含它,如下所示:
if (typeof jQuery == 'undefined') {
document.write('<script type="text/javascript" src=""jquery-1.3.2.min.js""></script>');
}
Then, it will only include it if jQuery isn't already on the page. There is one caveat, if you're using a different(probably newer) version of jQuery than the validation code was built for, there's a chance there are some breaks.
然后,它只会在页面上还没有 jQuery 时包含它。有一个警告,如果您使用的 jQuery 版本与构建的验证代码不同(可能更新),则可能会出现一些中断。
回答by webmaker1
i spent hours figuring out why using jQuery.noConflict() doesn't work as is...
我花了几个小时弄清楚为什么使用 jQuery.noConflict() 不能正常工作......
Just use
var j$ = jQuery.noConflict();
and after that use j$ in place of $ every time you use Jquery on that page.
var j$ = jQuery.noConflict();
每次在该页面上使用 Jquery 时,只需使用
,然后使用 j$ 代替 $ 。
回答by user1515295
Using an iframe will cause JQuery to load twice if you
1) load JQuery for each page (i.e. in Ruby on Rails via application.html.erb) and
2) the iframe is also a page from your application rather than an iframe of an external site.
如果您
1) 为每个页面加载 JQuery(即通过 application.html.erb 在 Ruby on Rails 中)和
2)iframe 也是来自您的应用程序的页面而不是外部的 iframe,则使用 iframe 将导致 JQuery 加载两次地点。