Forum Moderators: open

Message Too Old, No Replies

Is this a waste of 17 characters?

Question about potentially useless code

         

hmmdinger

6:31 pm on Sep 29, 2004 (gmt 0)

10+ Year Member



Here is the line of code:

<body onload="void(0);">

I'm no expert, but isn't this onload action doing NOTHING? Is there any possible reason for it to be there?

Many thanks for the help!

StupidScript

7:28 pm on Sep 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What happens when you remove it?

hmmdinger

7:33 pm on Sep 29, 2004 (gmt 0)

10+ Year Member



Good question. Nothing happens, but with a complex e-commerce site, I'd feel a little better knowing what that little bit o' code might be TRYING to do, you know?

I'm not a programmer, so just because the page doesn't APPEAR to break to me, doesn't tell me I am doing something bad by removing it.

encyclo

7:40 pm on Sep 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com], hmmdinger.

I would guess that the above onload does nothing, but I'd be careful before removing it. Depending on how the system is built, perhaps on some pages there is an onload event needed. The system might be placing either the real action on pages that require it, or the void() action if not. So if you remove the onload completely in the template, perhaps that would break the onload on certain specific pages.

Personally, I'd leave it in unless you're really sure - 17 bytes per page won't break the bandwidth budget, but a broken page on the site could cause big problems.

StupidScript

7:15 pm on Sep 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree with encyclo. Here is the definition from devedge.netscape.com:
The void operator is used in either of the following ways:

1. void (expression)
2. void expression

The void operator specifies an expression to be evaluated without returning a value. expression is a JavaScript expression to evaluate. The parentheses surrounding the expression are optional, but it is good style to use them.

Implemented in
JavaScript 1.1

ECMA version
ECMA-262

You can use the void operator to specify an expression as a hypertext link. The expression is evaluated but is not loaded in place of the current document.

The following code creates a hypertext link that does nothing when the user clicks it. When the user clicks the link, void(0) evaluates to 0, but that has no effect in JavaScript.

<A HREF="javascript:void(0)">Click here to do nothing</A>

The following code creates a hypertext link that submits a form when the user clicks it.

<A HREF="javascript:void(document.form.submit())">Click here to submit</A>

Perhaps it is in the onload event to quash any error messages that might otherwise pop up? Just musing ...

bird

8:57 pm on Sep 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That code in itself does nothing. But it *might* prevent something else from happening.

I'm not savvy on the sequence of processing when a page is loaded, so this is a shot in the dark, but here's the general idea anyway: An "onload" handler may be installed in several ways. One is the onload attribute to the body tag, our example. Another is a statement "window.onload=func;" in a JavaScript file. There are probably others.

Now if both of those (or others) are present when a page is loaded, then one of them must take precedence. If(!) the tag attribute takes precedence, then our example would prevent a JavaScript injected onload function to take hold. The site may have such JavaScript in place, which is normally desired, but disabled like this on a few pages.

As I said, this is the most reasonable speculation I could come up with, but it may be worth checking the specifications to verify or disprove it.

choster

9:22 pm on Sep 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yet another possibility-- the page is generated by a content management system which was designed to require onLoad, but that capability is not desired on the page in question. It would be far from the most obtuse thing I've seen in CMS design.

Bernard Marx

9:31 pm on Sep 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that's what bird was saying. It sounds like the best / only explanation. There is no way to remove an event handler that has been set with the 'traditional' javascript assignment. It can only be overriden with an assignment to a function that does nothing, which is essentially what this is.

hmmdinger

9:55 pm on Sep 30, 2004 (gmt 0)

10+ Year Member



Thanks for everybody's help on this. I suspect most times (if not all) the code is not doing anything. Many pages are created by altering older pages, which may have had/needed certain code, with the new page NOT needing it. But as someone pointed out, it's hardly worth the few bytes to risk breaking it by removing it from all pages.

Would a "content-management system" be GoLive? That is what we are using.

I could always just remove the code as I work on individual pages, and test them, but even with that strategy, it may not be worth thoroughly testing all functionality of each page just to see if it was needed at all.

Just something that was bugging me. Thanks again...