0

I use the JQuery plugin a lot in my work, and at the top of each page it gets loaded with the following script tag:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

However, sometimes I go offline, and still want to work, so I cannot load the script from this location.

While it is possible to alter all of the script tags to point to a local copy, this is annoying.

I thought that a neat solution would be to redirect any requests to http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js to localhost/home/olly/some_folder/jquery.min.js

I originally wondered about trying to play with /etc/hosts, but this can't do what I need.

Is there any other way that doesn't require installing extra software that will run all the time?

I should also note that I do not want to store a local copy of the file on the server where this website is uploaded, for various reasons.

Oliver Fawcett
  • 583
  • 1
  • 6
  • 18

1 Answers1

0

Well, you have 3 4 possibilities:

  1. (1) With pure javascript:
    • Add the script tag programmatically
    • When no internet-connection, use localhost, else google

(.)

var local = "http://localhost/your_path/jquery.min.js"
var remote = "http://ajax.googleapis.com/.../jquery.min.js"
var actual = navigator.onLine ? remote : local;



var head = document.getElementsByTagName("head")[0];
var s = document.createElement("script");
s.type = "text/javascript";
s.src = actual;
head.appendChild(s);

if you need css as well, use this:

s = document.createElement("style");
s.type = "text/css"
s.src = "yyy.css";
head.appendChild(s);
  1. (2) Install nginx and add googleapis.com to your /etc/hosts file, pointing it to 127.0.0.1.

Then you set up virtual name based hosting for the domain ajax.googleapis.com and have a folder /ajax/libs/jquery/1.7.2 with file jquery.min.js in the root folder of the virtual name-based application.

  1. (3) Write out the script URL with a server-side framework, like ASP.NET/PHP
    For this, you might want to know how to test for an internet connection with WinAPI

PS: or 4. Set up a name-server, add it with ip 127.0.0.1 to resolv.conf, and put googleapis.com in there resolving to 127.0.0.1. Set the nameserver to start manually. When you're offline, start the nameserver, and it will redirect to localhost every time you call ajax.googleapis.com ;) Bind9 or powerdns would be good nameservers for this. powerdns is somewhat tricky to setup with database however. Bind9 is nicer with zone files, but ugly when you want DLZ (database).

Community
  • 1
  • 1
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442