Skip to content

Instantly share code, notes, and snippets.

@yosimox
Last active September 30, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yosimox/1778742 to your computer and use it in GitHub Desktop.
Save yosimox/1778742 to your computer and use it in GitHub Desktop.
JavaScript Loading Tool. Async loading and Syncloading with jquery and google analytics
/*
JavaScript Loading Tool
Async loading and Syncloading with jquery and google analytics
*/
(function() {
/* THE WAY OF FILE SETTING
CASE : javascript file
{
'type':'js', // put 'js
'src':'hidouki.js', // put the path of the js file
'async':true, // async -> true, sync -> false
'charset':'UTF-8' // *optional. put the encoding of the js file
}
CASE : Use Google Analytics
{
'type':'ga', // put 'ga'
'set': true, // put true for begin. if you'd like to stop measureing, put false.
'account':'UA-1111111-11' // put the account number such as 'UA-XXXXXX-XX'
}
CASE : Use jquery
{
'type':'jquery', // put 'jquery'
'set': true, // put true for begin. if you'd like to stop using, put false.
'version':'1.7.1' // *optional. put the jQuery version you'd like to use. if you don't provide any here, the version will be 1.7.1
}
*/
//file settings
var files = [
//this is a sample of setting. change here
{'type':'jquery', 'set':true, 'version':'1.7.1' },
{'type':'js', 'src':'hidouki.js', 'async':true, 'charset':'UTF-8'},
{'type':'js', 'src':'douki.js', 'async':false, 'charset':'Shift_JIS'},
{'type':'js', 'src':'hidouki2.js', 'async':true, 'charset':'Shift_JIS'},
{'type':'js', 'src':'douki2.js', 'async':false},
{'type':'ga', 'set': true, 'account':'UA-1111111-11'}
]
// Objects for loading Files
var loadingMachine = function(files){
this.files = files;
this.cnt = 0;
this.max = (this.files).length;
}
loadingMachine.prototype = {
//async loading
'getAsync' : function(file_obj){
var self = this;
setTimeout(function(){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = file_obj.src;
script.async = file_obj.async;
script.charset = file_obj.charset;
var elm = document.getElementsByTagName('script')[0]; elm.parentNode.insertBefore(script, elm);
},0);
self.cnt++;
self.main_loop(self.cnt);
},
//sync loading
'getSync' : function(file_obj){
var self = this;
var makeElem = function(scrp){
var elm = document.body || document.documentElement;
elm.appendChild(scrp);
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = file_obj.src;
script.async = file_obj.async;
if(file_obj.charset){
script.charset = file_obj.charset;
}
var elm = document.body || document.documentElement;
elm.appendChild(script);
script.onload = function(){self.cnt++; self.main_loop(self.cnt);}
script.onreadystatechange = function(){
switch(script.readyState){
case 'complete':
case 'loaded' :
script.onload();
break;
}
}
},
//Google Analytics Defalut Tag
'gaDef' : function(account){
window._gaq = window._gaq || [];
_gaq.push(['_setAccount', account]);
_gaq.push(['_trackPageview']);
var src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var file = {'type':'js', 'src':src, 'async':true};
this.getAsync(file);
},
//jQuery Loading
'getJquery' : function(ver){
var ver = ver || "1.7.1";
var src = "https://ajax.googleapis.com/ajax/libs/jquery/" + ver + "/jquery.min.js"
var file = {'type':'js', 'src':src, 'async':false};
this.getSync(file);
},
//main loop
'main_loop' : function(cnt){
//console.log("loop_" +cnt);
if(typeof files[cnt] === "undefined"){
return false;
}else{
switch (this.files[cnt].type){
case "js":
if(this.files[cnt].async === true){
this.getAsync(this.files[cnt]);
}else if(this.files[cnt].async === false){
this.getSync(this.files[cnt]);
}else{
console.log("error");
}
break;
case "ga":
if(this.files[cnt].set === true){
this.gaDef(this.files[cnt].account);
}
break;
case "jquery":
if(this.files[cnt].set === true && typeof jQuery === "undefined"){
this.getJquery(this.files[cnt].version);
}
break;
}
}
}
//end of prototype
}
//begin
var l = new loadingMachine(files);
l.main_loop(0);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment