Load jQuery from a CDN but fallback on local version
-
Hi! π
I was looking around for a script that allows me to have jQuery loaded from Google’s (or jQuery’s) CDN but with a fallback in case the remote file isn’t reachable.
I found out this topic, but it’s unfortunately closed and doesn’t give me much infos:
http://wordpress.org/support/topic/jquery-from-google-cdn-fall-back-to-wp-local?replies=7At the moment I use this in my functions.php:
// load jQuery directly from its original CDN add_filter('script_loader_src', 'my_script_loader_src', 10, 2); function my_script_loader_src($src, $handle) { if ($handle == "jquery") return "http://code.jquery.com/jquery-latest.min.js"; return $src; }
… that works just fine, the only problem is that doesn’t have the fallback.
For the fallback I found out this “standalone” (non-WP) script that seems to work:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript"> if (typeof jQuery == 'undefined') { document.write(unescape("%3Cscript src='/js/jquery-latest.min.js' type='text/javascript'%3E%3C/script%3E")); } </script>
…anyway I don’t know how to “mix” this latest one with the “WP version”.
Can you help me? Thanks!!! π
- The topic ‘Load jQuery from a CDN but fallback on local version’ is closed to new replies.