Mick White wrote:
Brian K wrote: Thanks, but as I said, I cannot edit the onload event handler itself...
...
window.onload=myFunc
</script>
</head>
<body>
...
And if the onload event is already defined on the page, that line, if placed
in the <head>, will be replaced by the onload event defined in the <body>. If
placed in the <body>, it will replace any existing onload event for the page.
Injecting code for execution onload when you have no other control over the
page is problematic at best. The following works in IE6SP1, Firefox 0.9.1,
Mozilla 1.7, Opera 7.5.2 and Netscape 4.78 on an x86 under Windows XP. I make
no claims about other browsers on other platforms.
<head>
<script type="text/javascript">
function appendOnloadEvent(eventHandler) {
if (window.onload) {
// there is currently an onload event defined for
// window; append the event handler javascript to
// the current event handler javascript
window.onload = new Function(
// take the original event
window.onload
// convert it to a string; the string looks like:
// "...function...()...{...[js]...}..."
.toString()
// remove any newlines or carriage returns
.replace(/[\r\n]/g, '')
// remove any leading or trailing whitespace
.replace(/\s+$|^\s+/g, '')
// turn "function...()...{...[js]...}"
// into "function...()...{...[js][newline][new js]...}"
.replace(
/^function\s*\w+\s*\(\w*\)\s*\{\s*(.*)\s*\}$/,
'$1\n' + eventHandler
)
);
} else {
// there is currently no onload event defined for this
// window; set the event handler javascript to handle
// the event
window.onload = new Function(eventHandler);
}
}
function test() {
alert('bye');
}
</script>
</head>
<body onload="alert('hi');">
<script type="text/javascript">appendOnloadEvent('test();');</script>
</body>
But even it requires script injection inside the <body></body> to work
properly.
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ -
http://jibbering.com/faq