summaryrefslogtreecommitdiff
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
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
-rw-r--r--_test/cases/lib/exe/js_js_compress.test.php5
-rw-r--r--lib/exe/js.php8
-rw-r--r--lib/scripts/events.js55
3 files changed, 59 insertions, 9 deletions
diff --git a/_test/cases/lib/exe/js_js_compress.test.php b/_test/cases/lib/exe/js_js_compress.test.php
index 48b36af15..f46cbe227 100644
--- a/_test/cases/lib/exe/js_js_compress.test.php
+++ b/_test/cases/lib/exe/js_js_compress.test.php
@@ -19,6 +19,11 @@ class js_js_compress_test extends UnitTestCase {
$this->assertEqual(js_compress($text), 'var foo=6;');
}
+ function test_mlcomcond(){
+ $text = '/*@if (@_win32)';
+ $this->assertEqual(js_compress($text), '/*@if(@_win32)');
+ }
+
function test_slcom1(){
$text = '// an comment';
$this->assertEqual(js_compress($text), '');
diff --git a/lib/exe/js.php b/lib/exe/js.php
index de996a117..75b9b1f1c 100644
--- a/lib/exe/js.php
+++ b/lib/exe/js.php
@@ -143,11 +143,11 @@ function js_out(){
js_runonstart('scrollToMarker()');
js_runonstart('focusMarker()');
- // initialize init pseudo event
+/* // initialize init pseudo event
echo 'if (document.addEventListener) {'.NL;
echo ' document.addEventListener("DOMContentLoaded", window.fireoninit, null);'.NL;
echo '}'.NL;
- echo 'addEvent(window,"load",window.fireoninit);'.NL;
+ echo 'addEvent(window,"load",window.fireoninit);'.NL;*/
// end output buffering and get contents
$js = ob_get_contents();
@@ -245,8 +245,8 @@ function js_compress($s){
while($i < $len){
$ch = $s{$i};
- // multiline comments
- if($ch == '/' && $s{$i+1} == '*'){
+ // multiline comments (keeping IE conditionals)
+ if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){
$endC = strpos($s,'*/',$i+2);
if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR);
$i = $endC + 2;
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(){};