--- layout: main title: Head.JS, the only script in your HEAD ---
Need to display an element at a specific screen size ?
{% highlight js %} /* JS */ if (head.screen.innerWidth > 800) { document.getElementById("#side-menu").style.display = "block"; } /* CSS */ .gt-800 #side-menu { display: block; } {% endhighlight %}As you can see above, if you can write it in CSS, you can usually also write it in JavaScript.
What about custom detections and device/browser features ?
{% highlight css %} /* Add a custom feature yourself */ head.feature("member", true); /* Now use it */ if (head.member) { document.querySelector(".member-menu").style.display = "block"; } .member-menu { display: block; } /* Use built-in detections*/ /* if the browser supports CSS3 box-shadow */ if (head.boxshadow) { document.querySelector(".member-menu").style.boxShadow = "3px 3px 3px black"; } /* if the browser supports CSS3 box-shadow */ .boxshadow .member-menu { box-shadow: 3px 3px 3px black; } {% endhighlight %}We have a bunch of features detections built in, and if that's not enough, simply create one yourself !
Need to quickly load a JS or CSS file ?
{% highlight js %} // Load up some JS head.load("jQuery.js", function() { // Call a function when done console.log("Done loading jQuery"); }); // Load up some CSS head.load("bootstrap.css"); {% endhighlight %}On DocumentReady, if the user is using IE8, you need to load 2 scripts, or otherwise load a different script, then wait for them to finish loading, then fire a callback, and then load/apply a stylesheet ? Yeah right..
Easy.
{% highlight js %} // head.ready(document, callback) // head.test(test, success, failure, callback) head.ready(document, function() { head.test( (head.browser.ie && head.browser.version === 8), ["file1.js", "file2.js"], ["other.js"], function() { // run callback head.load("file1.css"); } ); }); {% endhighlight %}Of course you can use each of those functions (ready, test, load) individually too.