How to include a javascript/jquery file once


When creating modules or widgets, you may want to make sure the dependent .js is included, but you don't want it added twice, so here's a quick jQuery dependent solution to add files only once.

<script>
//define var to hold includes
var includedFiles = [];
//function to include js only once
function includeOnce(file){
if( typeof includedFiles.file === 'undefined' ){
$.getScript(file);
includedFiles.file = true;
}
}
</script>




Shane Stebner

Author: Shane Stebner

Shane is a successful web-developer specializing in responsive design, and the open-source LAMP stack. Over the years, he's gained in-depth experience with Stripe, Twilio, PhantomJs, Authorize.net, PayPal, Facebook, Twitter, and many other plugins/APIs.


comments powered by Disqus