summaryrefslogtreecommitdiff
path: root/lib/scripts
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2006-06-16 12:45:39 +0200
committerAndreas Gohr <andi@splitbrain.org>2006-06-16 12:45:39 +0200
commit5cafff964bdbfadfa66e4752eb6b392d2fce9087 (patch)
treed542d0dd06174b786b90e78adfdaf09749d96709 /lib/scripts
parentf0ae5d4c73ebc531da690dcc7a9f21c00700af54 (diff)
downloadrpg-5cafff964bdbfadfa66e4752eb6b392d2fce9087.tar.gz
rpg-5cafff964bdbfadfa66e4752eb6b392d2fce9087.tar.bz2
better onload handling
This patch improves the way the window.oninit JavaScript function is called. This function is used to initialiaze all JavaScript funcions attached to the DOM so it needs to be executed **after** the full DOM was parsed by the browser. Unfortunately currently only Mozilla supports a DOMContentLoaded event. In all other browsers we had to wait for the window.onload event which will only be called after **all** content (including images) was loaded - this caused a visible delay on all JavaScript generated content (like the toolbar) in non-Mozilla browsers. Dean Edwards now presented a solution [1] which will work for all the bigger Browsers and is used in this patch. The following browsers now should fire the init event right after parsing the DOM: All Mozilla based browsers Internet Explorer Safari Opera > darcs-hash:20060616104539-7ad00-db70d31fcb21cb812cf4982fe80a7d649e2daa1c.gz
Diffstat (limited to 'lib/scripts')
-rw-r--r--lib/scripts/events.js55
1 files changed, 50 insertions, 5 deletions
diff --git a/lib/scripts/events.js b/lib/scripts/events.js
index 262e5ec2d..31a81f6da 100644
--- a/lib/scripts/events.js
+++ b/lib/scripts/events.js
@@ -67,26 +67,71 @@ fixEvent.stopPropagation = function() {
* on window load at last.
*
* @author based upon some code by Dean Edwards
- * @author Andreas Gohr
- * @see http://dean.edwards.name/weblog/2005/09/busted/
+ * @author Dean Edwards
+ * @link http://dean.edwards.name/weblog/2006/06/again/
*/
window.fireoninit = function() {
// quit if this function has already been called
if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
arguments.callee.done = true;
+ // kill the timer
+ if (_timer) {
+ clearInterval(_timer);
+ _timer = null;
+ }
- if (typeof window.oninit == 'function') {
+ if (typeof window.oninit == 'function') {
window.oninit();
}
};
/**
- * This is a pseudo Event that will be fired by the above function
+ * Run the fireoninit function as soon as possible after
+ * the DOM was loaded, using different methods for different
+ * Browsers
+ *
+ * @author Dean Edwards
+ * @link http://dean.edwards.name/weblog/2006/06/again/
+ */
+ // for Mozilla
+ if (document.addEventListener) {
+ document.addEventListener("DOMContentLoaded", window.fireoninit, null);
+ }
+
+ // for Internet Explorer (using conditional comments)
+ /*@cc_on @*/
+ /*@if (@_win32)
+ document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
+ var script = document.getElementById("__ie_onload");
+ script.onreadystatechange = function() {
+ if (this.readyState == "complete") {
+ window.fireoninit(); // call the onload handler
+ }
+ };
+ /*@end @*/
+
+ // for Safari
+ if (/WebKit/i.test(navigator.userAgent)) { // sniff
+ var _timer = setInterval(function() {
+ if (/loaded|complete/.test(document.readyState)) {
+ window.fireoninit(); // call the onload handler
+ }
+ }, 10);
+ }
+
+ // for other browsers
+ window.onload = window.fireoninit;
+
+
+/**
+ * This is a pseudo Event that will be fired by the fireoninit
+ * function above.
*
* Use addInitEvent to bind to this event!
*
- * @author Andreas Gohr
+ * @author Andreas Gohr <andi@splitbrain.org>
+ * @see fireoninit()
*/
window.oninit = function(){};