Created
October 11, 2013 09:30
-
-
Save bondarewicz/6932069 to your computer and use it in GitHub Desktop.
JS: widget.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
// Localize jQuery variable | |
var jQuery; | |
/******** Load jQuery if not present *********/ | |
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') { | |
var script_tag = document.createElement('script'); | |
script_tag.setAttribute("type","text/javascript"); | |
script_tag.setAttribute("src", | |
"http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"); | |
if (script_tag.readyState) { | |
script_tag.onreadystatechange = function () { // For old versions of IE | |
if (this.readyState == 'complete' || this.readyState == 'loaded') { | |
scriptLoadHandler(); | |
} | |
}; | |
} else { | |
script_tag.onload = scriptLoadHandler; | |
} | |
// Try to find the head, otherwise default to the documentElement | |
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag); | |
} else { | |
// The jQuery version on the window is the one we want to use | |
jQuery = window.jQuery; | |
main(); | |
} | |
/******** Called once jQuery has loaded ******/ | |
function scriptLoadHandler() { | |
// Restore $ and window.jQuery to their previous values and store the | |
// new jQuery in our local jQuery variable | |
jQuery = window.jQuery.noConflict(true); | |
// Call our main function | |
main(); | |
} | |
/******** Our main function ********/ | |
function main() { | |
jQuery(document).ready(function($) { | |
/******* Load CSS *******/ | |
var css_link = $("<link>", { | |
rel: "stylesheet", | |
type: "text/css", | |
href: "style.css" | |
}); | |
css_link.appendTo('head'); | |
/******* Load HTML *******/ | |
var jsonp_url = "http://www.bondarewicz.com/js-widget/jsonp.php?callback=?"; | |
$.getJSON(jsonp_url, 'html_element=form', function(data) { | |
$('#example-widget-container').html("This data comes from another server: " + data.html_element); | |
}); | |
}); | |
jQuery('body').append('<div id="example-widget-container"></div>'); | |
} | |
})(); // We call our anonymous function immediately |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment