Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

451
Views
Can I limit scope of jQuery version I need using noConflict() if other scripts' loading order is unknown?

I want to asynchronously include a specific version of jQuery in a page I don't control (e-commerce platform) and only use it in my script. The page may load other scripts, which may also want to do the same thing, and I don't know the order in which the page includes my script vs others.

Using jQuery.noConflict([removeAll]), can I ensure that:

  1. my script gets the right version of jQuery
  2. I don't overwrite jQuery version for anyone else?

I think this question is different from most other multiple jQuery version questions because people make assumptions about script inclusion order and don't use asynchronous jQuery loading with callbacks.

Thanks!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Here's my attempt at this (could someone confirm this is ok?):

myscript.js

(function() {

  var myJQuery;

  function loadjQuery(url, callback) {
    var runCallbackOnSuccess = function() {
      if (typeof jQuery != "undefined") {
        myJQuery = jQuery.noConflict(true);   // NOTICE THIS LINE
        callback();
      }
    };
    var scriptTag = document.createElement("script");
    scriptTag.setAttribute("src", url);
    scriptTag.onload = runCallbackOnSuccess; 
    document.getElementsByTagName("head")[0].appendChild(scriptTag);
  }

  function doSomethingInMyScript() {
    myJQuery(document).ready(....);
  }

  loadjQuery("https://code.jquery.com/jquery-A.B.C.min.js", doSomethingInMyScript);
})();

otherscript.js (outside my control, but assuming it's like this)

(function() {
  function loadjQuery(url, callback) {
    <same as above, but without the noConflict() call>
  }

  function doSomethingInOtherScript() {
    jQuery(document).ready(....);
  }

  loadjQuery("https://code.jquery.com/jquery-X.Y.Z.min.js", doSomethingInOtherScript);
})();

Will this code work regardless of:

  1. whether the page includes myscript.js or otherscript.js first
  2. whether the callback functions for myscript.js or otherscript.js is executed first?

I suspect it should be fine as long as the callback functions are executed immediately after each respective jQuery is loaded, with no possibility of code from the other script interleaving between jQuery loaded and callback executed.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!