0

I'm already using this fallback method for a CDN hosted copy of jQuery, but now I want to do the same for jqMobile, if possible.

They offer their CDN up in the getting started guide, so that's obviously step 1. But if I'm going to use a similar solution from the jQuery include, I've got to check for the existence of a variable. What can I check for to decide whether or not the CDN jqMobile include was successful?

<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
<script type="text/javascript">
  if (typeof ?????? == 'undefined')
  {
    document.write(unescape("%3Cscript src='/path/to/your/jquery-mobile.js' type='text/javascript'%3E%3C/script%3E"));
  }
</script>
Community
  • 1
  • 1
Adam Tuttle
  • 19,505
  • 17
  • 80
  • 113

3 Answers3

1

I think its safe to test the $.mobile object:

if (typeof $.mobile == 'undefined')
  {
    document.write(unescape("%3Cscript src='/path/to/your/jquery-mobile.js' type='text/javascript'%3E%3C/script%3E"));
  }

Check it out here: http://jsfiddle.net/shanabus/7UKGN/1/

In that example, the url is broke so it tries to load it. After 5 seconds, it checks again.

shanabus
  • 12,989
  • 6
  • 52
  • 78
0

This seems to work, but I can't promise that it's a perfect solution:

<script src="http://codezzzzzz.jqueryzzzzz.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
<script type="text/javascript">if (typeof jQuery.widget == 'undefined'){document.write(unescape("%3Cscript src='assets/jquery.mobile-1.0.min.js' type='text/javascript'%3E%3C/script%3E"));}</script>

Note that the domain for the CDN copy is purposefully messed up to enable testing the fallback. It should be code.jquery.com.

Adam Tuttle
  • 19,505
  • 17
  • 80
  • 113
0

jQuery is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML. It was released in January 2006 at BarCamp NYC by John Resig. Used by over 49% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use today. you can refer here to know more about Jquery

coder
  • 1