CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: JavaScript

Dynamically loading JSON2.js if not loaded

611 Views   
if (!JSON) {
    // Autoload JSON2.js if possible
    // Note: if using a script loader you may have to explicitly load json2.js
    //       assumes loading from same folder as this script
    var scripts = $("script");
    var src = null;
    scripts.each(function (i) {
        src = $(this).attr("src");        
        if (src && src.toLowerCase().indexOf("serviceproxy.") > -1) {
            src = src.toLowerCase();
            src = src.substr(0, src.indexOf("serviceproxy.")) + "json2.min.js";            
            return false;
        }
    });           
    $("<script>").attr("src", src).attr("type", "text/javascript").appendTo("head");
}
 
// another option - based on last script loaded - simpler, but less reliable
 
if (!JSON) {
    // Autoload JSON2.js if possible
    var script = $("script").last();    
    var src = script.attr("src").toLowerCase();
    var src = src.substr(0, src.indexOf('serviceproxy.')) + "json2.min.js";
    $("<script>").attr("src", src).attr("type", "text/javascript").appendTo("head");
}
by Rick Strahl
  April 05, 2011 @ 9:57pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies