Jump to content
GreenSock

Noranterry

how show tweenmax.js from local if cdn fail?

Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi, actually have this: 

 

<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/1.10.1/TweenMax.min.js"></script>

 

so cdn could failed, which is the most optimal way to use my local copy?

 

<script type='text/javascript'>

  window.TweenMax || document.write('<script src="gsap/TweenMax.min.js">\x3C/script>')

</script>

 

Thanks in advance.

  • Like 1
Link to comment
Share on other sites

Hi and welcome to the forums.

 

Yes the document write is the most common resource, another is using JQuery's ajax, that comes with it's own document write callback, but for that I use one of my favourite libreries: YepNopeJS, you can use it to load JQuery if that CDN fails too.

 

It's very simple to use it:

<script src="localPath/js/yepnope.js"></script>

<script>
yepnope([{
    load: 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js',//First we add the CDN links
    complete: function ()
    {
        if (!window.jQuery)//if the CDN fails we load the local copy
        {
            yepnope('localPath/js/jquery.min.js');
        }
    }
},{
    load: 'http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js'
    complete: function ()
    {
        if(!window.TweenMax)//if the CDN fails we load the local copy
        {
            yepnope('localPath/js/gsap/TweenMax.min.js');
        }
    }
}]);
</script>

And you're done!!

 

The beauty of YeoNope: no libraries dependencies, asynchronous, has callbacks and you can use it to conditionally load CSS as well, so is a great resource for IE fallbacks and responsive design.

 

Hope this helps,
Cheers,

Rodrigo.

  • Like 4
Link to comment
Share on other sites

  • 4 years later...

Sorry for reviving this old topic, but question anno 2018:

How would you go about loading a local fallback today?

Link to comment
Share on other sites

I'd say the elder technique still holds true.

Link to comment
Share on other sites

Ok, just seemed that YepNope got deprecated for a reason - that being, other perhaps more viable options are available today, rather than having to load yet another library to check for this

Link to comment
Share on other sites

Sorry @mrsam I wasn't clear on my point (too early in the day...).

 

I did not mean to specifically use the YepNope library, but to sniff out a response to the call. You can make use of Promise and the Fetch api.

 

Sorry I can't be more specific - I haven't had to sniff out CDN failures myself so, I'm talking on a totally theoretical platform.

 

:D

 

  • Like 3
Link to comment
Share on other sites

Fetch is good, but it's asynchronous. The document.write method is solid as it will continue to load your scripts and other stuff in your html in the correct order. You could also do that to fallback to another CDN. Have you tried jsDelivr? I think it's much better than cdnjs.

https://www.jsdelivr.com

 

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×