Skip to content

Commit

Permalink
Added test for, and implemented, body-onload event firing/handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
gleneivey committed Jul 23, 2009
1 parent 301375c commit 2d68820
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 12 deletions.
18 changes: 14 additions & 4 deletions dist/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -4165,9 +4165,19 @@ __extend__(DOMDocument.prototype, {
_this._url = url;

$info("Sucessfully loaded document at "+url);
var event = document.createEvent();
event.initEvent("load");
$w.dispatchEvent( event );

// first fire body-onload event
var event = document.createEvent();
event.initEvent("load");
try { // assume <body> element, but just in case....
$w.document.getElementsByTagName('body')[0].
dispatchEvent( event );
} catch (e){;}

// then fire window-onload event
event = document.createEvent();
event.initEvent("load");
$w.dispatchEvent( event );
};
xhr.send();
},
Expand Down Expand Up @@ -7372,7 +7382,7 @@ $debug("Defining MouseEvent");
/*
* mouseevent.js
*/
$debug("Defining MouseEvent");
$debug("Defining UiEvent");
/*
* uievent.js
*/
Expand Down
18 changes: 14 additions & 4 deletions dist/env.rhino.js
Original file line number Diff line number Diff line change
Expand Up @@ -4602,9 +4602,19 @@ __extend__(DOMDocument.prototype, {
_this._url = url;

$info("Sucessfully loaded document at "+url);
var event = document.createEvent();
event.initEvent("load");
$w.dispatchEvent( event );

// first fire body-onload event
var event = document.createEvent();
event.initEvent("load");
try { // assume <body> element, but just in case....
$w.document.getElementsByTagName('body')[0].
dispatchEvent( event );
} catch (e){;}

// then fire window-onload event
event = document.createEvent();
event.initEvent("load");
$w.dispatchEvent( event );
};
xhr.send();
},
Expand Down Expand Up @@ -7809,7 +7819,7 @@ $debug("Defining MouseEvent");
/*
* mouseevent.js
*/
$debug("Defining MouseEvent");
$debug("Defining UiEvent");
/*
* uievent.js
*/
Expand Down
16 changes: 13 additions & 3 deletions src/dom/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,19 @@ __extend__(DOMDocument.prototype, {
_this._url = url;

$info("Sucessfully loaded document at "+url);
var event = document.createEvent();
event.initEvent("load");
$w.dispatchEvent( event );

// first fire body-onload event
var event = document.createEvent();
event.initEvent("load");
try { // assume <body> element, but just in case....
$w.document.getElementsByTagName('body')[0].
dispatchEvent( event );
} catch (e){;}

// then fire window-onload event
event = document.createEvent();
event.initEvent("load");
$w.dispatchEvent( event );
};
xhr.send();
},
Expand Down
2 changes: 1 addition & 1 deletion src/event/uievent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$debug("Defining MouseEvent");
$debug("Defining UiEvent");
/*
* uievent.js
*/
Expand Down
13 changes: 13 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,18 @@ <h2 id="userAgent"></h2>
</dl>

<ol id="tests"></ol>

<!-- test execution of main pages's body-onload tag functionality -->
<script>
bodyElem = document.getElementsByTagName('body')[0];
bodyElem.onload = function(){
var t = document.createTextNode(
'Look, a dynamically-generated paragraph!');
var p = document.createElement('p');
p.setAttribute('id', 'pCreatedByBodyOnload');
p.appendChild(t);
bodyElem.appendChild(p);
}
</script>
</body>
</html>
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ window.onload = function(){
load(
"test/unit/dom.js",
"test/unit/window.js",
"test/unit/onload.js",
"test/unit/parser.js",
"test/unit/timer.js",
//NOTE: keep this test last because Prototype pollutes
Expand Down
22 changes: 22 additions & 0 deletions test/unit/onload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* This file is a component of env.js,
* http://github.com/gleneivey/env-js/commits/master/README
* a Pure JavaScript Browser Environment
* Copyright 2009 John Resig, licensed under the MIT License
* http://www.opensource.org/licenses/mit-license.php
*/


module("window");

// depends on <script> block in test/index.html
test("Execution of body-onload in top-level document", function() {
// top-level window-onload works, or test framework wouldn't run.....
expect(1);

var mtch = document.getElementById('pCreatedByBodyOnload').innerHTML.
match(/dynamically-generated paragraph/);
try{ ok(mtch && mtch.length > 0,
"Got confirmation that body-onload handler executed");
}catch(e){print(e);}
});

0 comments on commit 2d68820

Please sign in to comment.