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

146
Visualizações
Loading javascript with a Promise fails on hard refresh

In my code I'm scanning a page to see if we find anything with a class of "coceon" (an image comparison JS lib), if found we then load in the cocoen scripts to then run it.

The code:

/* cocoen image comparison */
if ($('.cocoen').length)
{
    var cocoen_js = Promise.resolve($.cachedScript("/includes/jscripts/cocoen/js/cocoen.min.js"));
    var cocoean_jquery = Promise.resolve($.cachedScript("/includes/jscripts/cocoen/js/cocoen-jquery.min.js"));

    Promise.all([cocoen_js, cocoean_jquery]).then(() => { 
        $('.cocoen').cocoen();
      });
}

The trouble is, sometimes it just fails with the browser console telling me this line:

$('.cocoen').cocoen();

Fails as cocoen is not a function. So sometimes it's not loaded ready when needed. Especially so when doing a hard refresh, something seems to block it? Is there a way to ensure it's actually loaded to then run?

The cache script code if you need to see it too:

jQuery.cachedScript = function( url, options ) 
{
    // Allow user to set any option except for dataType, cache, and url
    options = $.extend( options || {}, {
      dataType: "script",
      cache: true,
      url: url
    });
   
    // Use $.ajax() since it is more flexible than $.getScript
    // Return the jqXHR object so we can chain callbacks
    return jQuery.ajax( options );
};
about 4 years ago · Juan Pablo Isaza
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