Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

validation - loading jquery twice causes an error?

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

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>');

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.

I suspect that error is coming up because validationdate.js is also loading jquery-1.3.2.min.js. Can this be an error?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...