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

203
Visualizações
Implement fallback incase original script is not found

My pages relies on this script currently:

script a)

<script  src="https://mipage.com/pathtoscript/owlscript.js"></script>

Incase loading this (script a) ) fails, I'd want to load:

script b)

<script  src="https://otherpage.com/pathtoscript/owlscript.js"></script>

Any way to do this ?

PS: I've seen implementations for jsquery, but those won't work here...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The best way for this is to listen for the error event of the first script tag. You can do this for example with the onerror property:

<script src="https://mipage.com/pathtoscript/owlscript.js" onerror="loadFallbackScript()"></script>

and then loading the fallback script from code:

function createScriptElement(src) {
    const script = document.createElement('script');
    script.src = src;

    return script;
}

function loadFallbackScript() {
    const fallbackScriptElement = createScriptElement('fallback.js');
    document.head.append(fallbackScriptElement);
}

But since this requires loadFallbackScript to be a global function a cleaner way would be to create the first script tag dynamically as well and attach the event listener from code:

// execute this instead of the <script> tag in the html
const externalScriptElement = createScriptElement('external.js');
externalScriptElement.addEventListener('error', loadFallbackScript);
document.head.append(externalScriptElement);
about 4 years ago · Juan Pablo Isaza 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