Question

I was wondering if I can have something like

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

and in case it is not available ...how can I load something like

<script src="../localjs/jquery.js"></script>

And same with CSS

I am asking because I work with CDN a lot, and sometimes I'm developing in Localhost and I am not able to connect to the internet and some scripts stop working, I was wondering if I can do this so I don't have to worry about changing them again and again between CDN and local files.

Était-ce utile?

La solution 2

Include the following, right after the opening <body> tag:

<body>
    <script>
    if (!jQuery) 
        document.write('<script src="/local/jquery.js"><\/script>');
    </script>

<!-- ... html ... -->

</body>

Autres conseils

You could do something like below:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../localjs/jquery.js">\x3C/script>')</script>

Further reading: CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top