Created
August 25, 2009 17:15
-
-
Save wilkerlucio/174842 to your computer and use it in GitHub Desktop.
JS Page Load Trigger
This file contains 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
/* | |
* Simple Page Load Trigger | |
* | |
* Copyright 2009 Wilker Lucio <wilkerlucio@gmail.com> | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
SimpleLoadTrigger = (function() { | |
var readyBound = false; | |
var alreadyRun = false; | |
var triggers = []; | |
return { | |
run: function() { | |
if (alreadyRun) return; | |
alreadyRun = true; | |
for (var i = 0; i < triggers.length; i++) { | |
triggers[i](); | |
} | |
}, | |
bind: function(callback) { | |
this.bindReady(); | |
triggers.push(callback); | |
}, | |
bindReady: function() { | |
if (readyBound) return; | |
readyBound = true; | |
var self = this; | |
var callback = function() { | |
self.run(); | |
}; | |
if (document.addEventListener) { | |
document.addEventListener("DOMContentLoaded", callback, false); | |
window.addEventListener("load", callback, false); | |
} else if (document.attachEvent) { | |
document.attachEvent("onreadystatechange", function() { | |
if (document.readyState === "complete") { | |
document.detachEvent("onreadystatechange", arguments.callee, false); | |
self.run(); | |
} | |
}); | |
document.attachEvent("onload", callback); | |
} | |
} | |
}; | |
})(); | |
on_ready = function(callback) { | |
SimpleLoadTrigger.bind(callback); | |
}; |
This file contains 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
/* | |
* Simple Page Load Trigger | |
* | |
* Copyright 2009 Wilker Lucio <wilkerlucio@gmail.com> | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
SimpleLoadTrigger=(function(){var readyBound=false;var alreadyRun=false;var triggers=[];return{run:function(){if(alreadyRun)return;alreadyRun=true;for(var i=0;i<triggers.length;i++){triggers[i]()}},bind:function(callback){this.bindReady();triggers.push(callback)},bindReady:function(){if(readyBound)return;readyBound=true;var self=this;var callback=function(){self.run()};if(document.addEventListener){document.addEventListener("DOMContentLoaded",callback,false);window.addEventListener("load",callback,false)}else if(document.attachEvent){document.attachEvent("onreadystatechange",function(){if(document.readyState==="complete"){document.detachEvent("onreadystatechange",arguments.callee,false);self.run()}});document.attachEvent("onload",callback)}}}})();on_ready=function(callback){SimpleLoadTrigger.bind(callback)}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment