Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

453
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda