0

How can I go about setting up a fallback script, using jQuery or any other method? Why does this method not work?

https://api.jquery.com/jQuery.getScript/#jQuery-getScript

I use the code below to set a fallback script in case I get a 404 or similar on my primary script.

  $.getScript( "https://cdnjs.cloudflare.com/ajax/libs/three.js/r123/three.min.js" )
    .fail(function( jqxhr, settings, exception ) {
      $.getScript( "https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js" )
    })

When I add some random characters to the first URL to simulate a situation where the script is unavailable, I expect my code to load the script from the second URL and continue execution, but I get this error instead

GET https://cdnjs.cloudflare.com/ajax/libs/three.js/r123/threes.min.js?_=1608231826270 net::ERR_ABORTED 404

And my code is halted.

This shouldn't be happening since all my code is in the .done() callback of the first $.getScript call.

2
  • i would go for $.ajax and use error function, after quick internetsearch it seem getscript does not have crossdomain errorhandling Commented Dec 17, 2020 at 20:48
  • stackoverflow.com/questions/13670421/… Commented Dec 17, 2020 at 23:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.