Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

457
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda