Skip to content

Instantly share code, notes, and snippets.

@remvee
Created June 4, 2013 10:01
Show Gist options
  • Select an option

  • Save remvee/5704894 to your computer and use it in GitHub Desktop.

Select an option

Save remvee/5704894 to your computer and use it in GitHub Desktop.
/*
Optionally load javascripts when some condition is met. This code
ensures the scripts are loaded sequencially to avoid dependency
problems.
In this case: when an element with id "yelp" exists load jquery and
execute some javascript (depending on jquery) to replace the
innerHtml of that element with "YELP".
*/
if (document.getElementById("yelp")) {
(function(scripts) {
var f = function(scripts) {
var e = document.createElement("script");
e.type = "text/javascript";
if (scripts[0].src) e.src = scripts[0].src;
if (scripts[0].text) e.text = scripts[0].text;
e.onload = e.onreadystatechange = function() {
if (this.done || (this.readyState && this.readyState != "loaded" && this.readyState != "complete")) return;
this.done = true;
if (scripts.length > 1) f(scripts.slice(1));
};
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(e, s);
};
f(scripts);
})([{src: "jquery.js"},
{text: "$('#yelp').html('YELP')"}]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment