Is there a way to use a CDN (for jQuery) and have an offline Web app (via HTML5 manifests)?

后端 未结 3 398
后悔当初
后悔当初 2021-01-31 05:59

I\'m beginning to look at HTML5s ability to allow for offline Web applications.

A while back I found that using a CDN worked well for my applications, so I\'ve been stic

相关标签:
3条回答
  • 2021-01-31 06:40

    the path of checking if online jquery exists and if not loading from local was answered here: How to load local script files as fallback in cases where CDN are blocked/unavailable?

    where there is a discussion and a couple of different approaches

    personally I like the html5boilerplate approach:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
    <script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script>
    

    you can check if the browser supports offline caching with modernizer http://www.modernizr.com/ the modernizer 2.0 version supports conditional loading , so you can use it to detect and load the needed resource with the following script (taken from diveintohtml5):

        if (Modernizr.applicationcache) {
      // window.applicationCache is available!
    } else {
      // no native support for offline :(
      // maybe try Gears or another third-party solution
    }
    

    but as I said before, I prefer the html5boilerplate method

    0 讨论(0)
  • 2021-01-31 06:46

    Actually, you can write your manifest that has a link to your jQuery CDN. Just like my app.manifest:

    CACHE MANIFEST
    # 2012-01-20:v4
    
    http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    

    That is HTML segment:

    <html manifest="app.manifest">
    <head>
        ....
    </head>
    <body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        ....
    </body>
    </html>
    

    It works to me.

    0 讨论(0)
  • 2021-01-31 06:51

    After weeks of thought I finally had an idea while driving home tonight.

    It's possible to check if a browser has offline support. You could then have an area that would ask a user if they'd like to enable offline support for an application. If they do, you load in your local copy of jQuery. If not, or offline support isn't available, then you just reference jQuery from a CDN.

    Or perhaps it's a separate page in the application. If they visit it it simply has a script element that pulls in your local jQuery file. Then the page either goes back one or the window is closed, depending upon the application. (I assume adding your local copy of jQuery wouldn't work on the main page, unless you removed jQuery from the existing page (overwriting the variables or otherwise?).)

    Depending upon how the browser(s) decide to hold onto offline content (files), there may potentially be an issue if the local jQuery file is only loaded on one page, and very rarely. Until the user is offline, of course, at which point it is called on (potentially) every page.

    0 讨论(0)
提交回复
热议问题