两次加载 jquery 会导致错误吗? [英] loading jquery twice causes an error?

查看:31
本文介绍了两次加载 jquery 会导致错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在加载 jquery + 其他 js 的页面:

I have a page where jquery + other js's is being loaded:

<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>
<%}%>

validationdate.js 是我构建的 js,用于检查日期和其他内容.它有这个作为第一行:

the validationdate.js is a mine built js that checks dates and other stuff. it has this as the first line:

document.write('<script type="text/javascript" src=""jquery-1.3.2.min.js""></script>');

问题:是在生产中吗..这个页面出错并给出一个 JS 错误.这会导致选项卡不可点击.此错误间歇性发生,我似乎无法在我的机器上重现它.两台机器都使用IE.Firefox 中也会发生错误,尽管在 Firefox 中没有出现一些 JS 错误.我什至有萤火虫,它也没有显示任何 JS 错误.

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.

我怀疑会出现错误,因为 validationdate.js 也在加载 jquery-1.3.2.min.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?

顺便说一下,我得到的 JS 错误是异常被捕获但未抛出 ..第 23 行",第 23 行紧随其后

By the way, the JS error I get is 'Exception caught but not thrown ..line 23' and line 23 is following

<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!!

推荐答案

首先回答你的问题,,两次包含 jQuery 会导致各种问题.

First to to answer your question, yes, including jQuery twice can cause all sorts of issues.

要修复它,这一行:

document.write('<script type="text/javascript" src=""jquery-1.3.2.min.js""></script>');

应该被包裹以检查jQuery是否已经存在,而不是盲目地再次包含它,就像这样:

Should be wrapped to check if jQuery already exists instead of blindly including it again, like this:

if (typeof jQuery == 'undefined') {  
  document.write('<script type="text/javascript" src=""jquery-1.3.2.min.js""></script>');
}

然后,它只会在页面上还没有 jQuery 时包含它.有一个警告,如果您使用的是 不同(可能更新)版本的 jQuery,而不是为验证代码构建的版本,则可能会出现一些中断.

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 会导致错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆