Using HREF and ONCLICK in an anchor tag doesnt seem to work

I’m trying to setup a page that uses JavaScript in a text link to submit a form:

<a href="do.htm" onclick="javascript:document.forms.Q01.submit();">

I also have added a fallback page in the href (do.htm) in case users have JavaScript disabled. But the JavaScript doesn’t seem to be working. When I click the link it always goes to the fallback page, and doesn’t submit the form.

I’ve tried swaping the order in the anchor tag (JS first, href second):

<a onclick="javascript:document.forms.Q01.submit();" href="do.htm">

and the same thing happens.

If I remove the href value:

<a href="#" onclick="javascript:document.forms.Q01.submit();">

the JS does execute properly.

How can I get the JS to execute by default for browsers that support it, and the href be the backup for browsers that dont support it?


“javascript:” should only be used when you want to put JavaScript code where you normally put URLs; not in event handlers.

You need to return false from the onclick handler to stop the normal page change from clicking on the link.

<a href="do.htm" onclick="javascript:document.forms.Q01.submit();[color=#FF0000]return false;[/color]">