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
108 views
in Technique[技术] by (71.8m points)

javascript - Two jQuery versions on the same page - problem

The reason for two jQuery versions on the same page is a little lengthy, but I find myself needing it as different sections of the site are controlled by different parties (it's complicated).

There are 2 script tags in the header:

<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery-cookie.min.js"></script>

There is then another version of jQuery in the footer. The jQuery in the footer has this line of code running right after it's script tag:

window.$j = jQuery.noConflict();

The issue I'm having is, $.cookie or jQuery.cookie, when being used, returns the error: "jQuery.cookie is not a function"

Is there a way to preserve the jquery-cookie variables as they seem to be lost once the second version is added?

question from:https://stackoverflow.com/questions/65941643/two-jquery-versions-on-the-same-page-problem

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

1 Reply

0 votes
by (71.8m points)

If you are using no conflict correctly, it should be working. Example loading cookie and seeing it is still defined.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
  console.log("$ 1 cookie", typeof $.cookie);
</script>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
var jQuery_2_0_2 = $.noConflict(true);
</script>
<script>
  console.log("$ 2 cookie", typeof $.cookie);
  console.log("jQ cookie", typeof jQuery_2_0_2.cookie);
  console.log($.fn.jquery, jQuery_2_0_2.fn.jquery);
</script>

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

...