/    Sign up×
Community /Pin to ProfileBookmark

Interesting window.onload behaviour…

This is a test page I made trying to figure out what’s going on behind the javascript scenes.

While the .htm produces output at first, it is being overriden as soon as the javascript executes the document.write() function after loading the page.[b]WHY?[/b]
How, or is it possible at all to [i] append [/i] the document using an ‘onload’ event?

Also in Mozilla Firefox this produces and endless loop of alerts…it seems that the ‘onload’ event is triggered over and over again by the document.write() function.
On the other hand in Internet Explorer this is not the case. The alert comes only once.

This is the page. [COLOR=red]WARNING[/COLOR] Endless loop in Mozilla Firefox
[URL=http://www.maciej64d.1go.dk/event/event_test.htm]http://www.maciej64d.1go.dk/event/event_test.htm[/URL]

[COLOR=darkblue]event_test.htm[/COLOR]

[CODE]
<html>
<head>
<script src=”event_test.js” type=”text/javascript”></script>
</head>

<body>

<p>dude</p><p>dude2</p>
simple text

</body>
</html>

[/CODE]

[COLOR=darkblue]event_test.js[/COLOR]

[CODE]

addEvent(window, ‘load’, pageInit);

function pageInit () {
alert(“Page Loaded…”);
document.write(“javascript writing..”);
document.close();

}

/* addEvent by Scott Andrew */
function addEvent(obj, evType, fn){
if (obj.addEventListener) {
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent) {
var r = obj.attachEvent(“on”+evType, fn);
return r;
} else {
return false;
}
}
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JPnycDec 21.2004 — document.write() is a method which creates a new html document. It cannot add to an existing one.
Copy linkTweet thisAlerts:
@maciej64authorDec 21.2004 — hmm...thank's for your tip!

I guess it might be the case that AFTER THE PAGE IS LOADED document.write() will create a new document, while this works perfectly, producing the expected effect:

[CODE]
<html>
<head>

</head>

<body>

<p>dude</p>
<script>
document.write("<p>dude2</p>");
</script>

simple text

</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@JPnycDec 21.2004 — It will always create a new document. And if it's called onload then it shouldn't fire until the page is loaded. You can, however, have the javascript spit out an entire webpage that way, although I dunno why you'd want to.
×

Success!

Help @maciej64 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...