...

View Full Version : HTML solution? > link to scripts on Google; local copy as fail-safe?



iynque
05-11-2009, 01:20 AM
I want to save a bit of bandwidth and trouble by linking to scriptaculous and prototype on Google, a la:


<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js'></script>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.2/scriptaculous.js'></script>

BUT, I would like to be able to specify a local copy of the scripts as a fail-safe alternative. Is there a simple HTML way to specify an alternate src property?

....and I've tried searching, but "alternate script source" and various permutations just lead to useless HTML basics. ...and not the kind of basic HTML I'm hoping to find! :D

venegal
05-11-2009, 01:51 AM
I don't think there's an HTML way, but there definitely is a Javascript one:

After those script includes, include another js file containing the following:

if(!$){
jsInclude = document.createElement("script");
jsInclude.type = "text/javascript";
jsInclude.src = 'local/path/to/prototype/prototype.js';
document.getElementsByTagName('head')[0].appendChild(jsInclude);
}

It checks if the "$" function is defined. If it is, prototype has already been loaded successfully. If it isn't it, it adds another <script> node to the DOM containing your local prototype version.

For scriptaculous you can do the same thing, you just have to replace "$" with something defined in scriptaculous.

iynque
05-11-2009, 10:58 PM
I was hoping for simple HTML, but this script is far more ingenious than what I was planning as an alternative!

Thanks! ;)

rnd me
05-11-2009, 11:08 PM
I don't think there's an HTML way, but there definitely is a Javascript one:


if(!$){
jsInclude = document.createElement("script");
jsInclude.type = "text/javascript";
jsInclude.src = 'local/path/to/prototype/prototype.js';
document.getElementsByTagName('head')[0].appendChild(jsInclude);
}


this will thrown an error and not complete if prototype has not loaded, so it won't add the backup. thus: it doesn't work...

use if(! window.$) to be safe, and it will work.

what a difference a window makes...

venegal
05-11-2009, 11:13 PM
Ah yes, sorry for that, and thanks for the correction.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum