Is there any way to prevent a body onload from running?

1,341 views
Skip to first unread message

brei...@gmail.com

unread,
Feb 27, 2007, 10:19:32 AM2/27/07
to greasemonkey-users
Hi,

It seems that it is not possible to prevent a piece of javascript from
running if it is put in a <body onload=" ">. A helpdesk website I'm
using every now and then is using this to set the focus after loading
the page.

The things I tried:

1.
var all;
all = document.evaluate("//body", document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < all.snapshotLength; i++) {
var t = all.snapshotItem(i);
t.onload = null;
}

2.
document.body.onload = null;

By putting an alert() call in the script I could see that my GM script
was executed before the body onload, but I could not prevent that
piece of Javascript from running at all...

I have used the first loop to prevent an onunload from running but I
think this event is already ready to be delivered and it is too late
for GM to prevent it.

In the mean time I'm just modifying the tag it is looking for, but I
find it a bit annoying that I can't prevent the code from running at
all.

Regards,
Berend.

Anthony Lieuallen

unread,
Feb 27, 2007, 3:03:25 PM2/27/07
to greasemon...@googlegroups.com
On 2/27/2007 10:19 AM, brei...@gmail.com wrote:
> ...
> t.onload = null;
> ...
> document.body.onload = null;
> ...

First, this is a good site to read for new user script authors:
http://wiki.greasespot.net/Main_Page

Then, specifically, from the links:
http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html
Pitfall #2

To solve, you need this, but note the warning at the top:
http://wiki.greasespot.net/UnsafeWindow

Lenny Domnitser

unread,
Feb 27, 2007, 6:04:22 PM2/27/07
to greasemon...@googlegroups.com
On 2/27/07, brei...@gmail.com <brei...@gmail.com> wrote:
> document.body.onload = null;

Problem:
http://wiki.greasespot.net/XPCNativeWrapper#Expando_Properties

(Ugly) solution:
location.href = 'javascript:void(document.body.onload = null);';

Berend Reitsma

unread,
Feb 28, 2007, 5:34:04 AM2/28/07
to greasemonkey-users
Hi,

I've tried the suggestions in the followups but up to now nothing worked.
As I said before I did successfully use a similar GM script to prevent
a body.onunload from running. I don't know why it does not work for
the body.onload

Attached is a small html file that mimics the body.onload usage from
the mentioned website. I would be grateful if somebody can come up
with a working userscript that prevents the alert () call in this html
file from being called.

I have tested Firefox 2.0.0.1 on Windows XP and Firefox 1.5.0.7 on Debian Linux.
Greasemonkey version 0.6.7.20070131.0

These are the UserScript constructs I've tried:

// ==UserScript==
// @name Remove onload
// @namespace none
// @description Remove onload
// @include */body_onload.html
// ==/UserScript==

alert('Greasemonkey called');

location.href = 'javascript:void(document.body.onload = null);';

unsafeWindow.document.body.onload = null;
unsafeWindow.document.body.load = null;

function my_func(evt) {
alert('my_func called');
evt.preventDefault();
}
document.body.addEventListener("load", my_func, true);
document.body.addEventListener("onload", my_func, true);

unsafeWindow.document.body.addEventListener("load", my_func, true);
unsafeWindow.document.body.addEventListener("onload", my_func, true);


Regards,
Berend.

body_onload.html

esquifit

unread,
Feb 28, 2007, 7:49:33 AM2/28/07
to greasemon...@googlegroups.com
The following one-line script does the job:

document.body.setAttribute('onload','');

brei...@gmail.com

unread,
Feb 28, 2007, 8:46:37 AM2/28/07
to greasemonkey-users

On Feb 28, 1:49 pm, esquifit <esqui...@googlemail.com> wrote:
> The following one-line script does the job:
>
> document.body.setAttribute('onload','');

Yes, this works fine. Am I correct to conclude that the 'onload' is
not an event? That seems strange...

Anyway, thanks a lot for your help.

Regards,
Berend.

esquifit

unread,
Feb 28, 2007, 2:12:43 PM2/28/07
to greasemon...@googlegroups.com
The event is actually 'load'. There are two ways to access this
event: through the HTML attribute "onload" of the BODY html tag, and
through the DOM property 'onload' of the body element. HTML
Attributes and DOM properties are not the same. See [1].

Actually, onload is a custom property (supported by Mozilla, sometimes
referred to as 'expando' see [3]) It is custom because it does not
form part of the interface HTMLBodyElement [4] which is the base of
the HTML BODY tag.

In this case, the DOM property 'onload' is not available to GM for the
reasons pointed out by Lenny [2]. However, the standard HTML
attribute is always available.

Or something like that ;-)

[1] http://xulplanet.com/ndeakin/archive/2004/12/22/
[2] http://wiki.greasespot.net/XPCNativeWrapper#Expando_Properties
[3] http://www.xulplanet.com/ndeakin/article/278/
[4] http://www.xulplanet.com/references/objref/HTMLBodyElement.html

2007/2/28, brei...@gmail.com <brei...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages