3

In our project we imported javascript files in two types.

1) Imported from local machine

EXAMPLE: <script type="text/javascript" src="js/jquery.js"></script>

2) Imported from CDN

EXAMPLE: <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>

Which is the best options to import java script files or css files.When i use from CDN how to manage when the internet is not available

Thanks in advance...

Awais
  • 4,752
  • 4
  • 17
  • 40
PSR
  • 39,804
  • 41
  • 111
  • 151
  • It is completely your wish. I think there is no better or worst way. If you have internet use cdn/local or if you are working offline use local. – Mr_Green Jun 20 '13 at 09:20
  • Duplicate of http://programmers.stackexchange.com/questions/139372/referencing-external-javascript-vs-hosting-my-own-copy – Florian Margaine Jun 20 '13 at 09:25
  • possible duplicate of [Benefits vs. Pitfalls of hosting jQuery locally](http://stackoverflow.com/questions/3832446/benefits-vs-pitfalls-of-hosting-jquery-locally) – lonesomeday Jun 20 '13 at 11:39

2 Answers2

4

You could do something like this

for jQuery

<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>

<script>
  if(typeof window.jQuery === 'undefined') {
    document.write(
      unescape("%3Cscript src='js/jquery.js' type='text/javascript'%3E%3C/script%3E")
    );
  }
</script>

or this works

<script>
   !window.jQuery && document.write('<script src="js/jquery.js"><\/script>')
</script>

What this does is checks if jQuery exists if not it loads a local javascript file

just change to suit your needs

iConnor
  • 19,997
  • 14
  • 62
  • 97
0

When you use from CDN if you want to make your page available even when its offline declare a manifest file so that browser caches your file.

<html manifest="path/filename.manifest">

Put above attribute to your <html> Your manifest file looks something like this.

CACHE MANIFEST
#one-line-comment
CACHE:
http://code.jquery.com/ui/1.9.0/jquery-ui.js
#files that are to be loaded from cache.
NETWORK:
*
#files to be loaded online.
# * means all.
eldos
  • 3,132
  • 3
  • 29
  • 32