Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question HTML solution? > link to scripts on Google; local copy as fail-safe?

    I want to save a bit of bandwidth and trouble by linking to scriptaculous and prototype on Google, a la:

    Code:
    <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!

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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:
    Code:
    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.

  • Users who have thanked venegal for this post:

    iynque (05-11-2009)

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Excellent!

    I was hoping for simple HTML, but this script is far more ingenious than what I was planning as an alternative!

    Thanks!

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,346
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by venegal View Post
    I don't think there's an HTML way, but there definitely is a Javascript one:

    Code:
    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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Ah yes, sorry for that, and thanks for the correction.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •