Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail

前端 未结 23 1847
太阳男子
太阳男子 2020-11-21 07:19

What would be a good way to attempt to load the hosted jQuery at Google (or other Google hosted libs), but load my copy of jQuery if the Google attempt fails?

I\'m n

23条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 08:02

    This seems to work for me:

    
    
    
    
    
    
        

    hello jQuery

    The way it works is to use the google object that calling http://www.google.com/jsapi loads onto the window object. If that object is not present, we are assuming that access to Google is failing. If that is the case, we load a local copy using document.write. (I'm using my own server in this case, please use your own for testing this).

    I also test for the presence of window.google.load - I could also do a typeof check to see that things are objects or functions as appropriate. But I think this does the trick.

    Here's just the loading logic, since code highlighting seems to fail since I posted the whole HTML page I was testing:

    if (window.google && window.google.load) {
        google.load("jquery", "1.3.2");
    } else {
        document.write('
    
                                     
                  
提交回复
热议问题