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

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

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 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