Category Archives: JavaScript

Form#submit Failure in Internet Explorer 6

If you are going to submit a form through the anchor tag <a>, watch out for the href attribute. the HTTP response could be rejected by Internet Explorer 6 if you use something like javascript:void(0) as the value of the href attribute. For example, with an anchor tag like this:

<a id="bar" href="javascript:void(0)" onclick="onFoo();">Foo</a>

and with onFoo() handling the form submission (i.e. call submit() on the form object), the HTTP response will not be handled by Internet Explorer 6.

One may suggest replacing javascript:void(0) with #:

<a id="bar" href="#" onclick="onFoo();">Foo</a>

However, this will most likely cause the client to scroll to the top of the page. Instead, use a hash (dummy value) as the anchor name which is not defined anywhere in the document. The client will not scroll up this time.

<a id="bar" href="#doesnotexist" onclick="onFoo();">Foo</a>

Notice that this approach will cause the # sign and the hash to be appended to the URL shown on some clients’ address bars. If you do not like this, add return false; to the onclick event, which prevents the original behavior of the element from functioning (i.e. the hyperlink no longer redirects).

<a id="bar" href="can_be_anything_here" onclick="onFoo(); return false;">Foo</a>

This only works if the onFoo() function does not throw an error.

If the only function you want to implement with the hyperlink is to submit a form (execute onNext()), just get rid of the onclick attribute and go for href.

<a id="bar" href="javascript:onFoo();">Foo</a>