Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Javascript links and submit buttons

A trick - for non javascript browsers

12:46 am on Jun 28, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 4, 2001
votes: 0

I thought I'd share this little tidbit with you all as I just ran across it. Lets say you have a link that you want to load say a popup window with or do something w/ javascript and then load the link. Normally, you'd do something like this:
<a href="#" onClick="popupPage('something.html')">text</a>

But this fails for the non-javascript browsers for obvious reasons. A neat thing you can do is the following:
<a href="something.html" onClick="popupPage('something.html');return false;" target="_blank">text</a>

The return false in the javascript tells the browser to stop performing other actions associated w/ that event, such as opening something.html a second time. But if js is turned off, there will be no return false and something.html will still be loaded in a new window.

This is also great for links where you want a j/s popup box that says something like "Are you sure, yes/no". Just return true/false back to the onClick parameter and without j/s, it defaults to 'yes'. This also works for onClick's for submit buttons in forms. I hate all those submit buttons where you can only submit if j/s is enabled b/c of client side validation code.

Anyway, I just ran into this. Perhaps it isn't news to many of you, I dont know.

7:03 am on June 28, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:June 7, 2002
votes: 0

Yup, I've used the popup trick myself.

But for form submissions, I wouldn't pop up an "are you sure?" dialogue. It's far more effective to return a copy of the filled-in form and provide buttons to "Submit" and "Edit". Users are more likely to spot errors that way.

Arrange for the form validation script to return true for a correctly filled-in form, and false if it contains errors. Then include the following event handler in the <form> tag:

onsubmit="return check(this);"

Without JavaScript, the form will be submitted, but possibly with errors -- it goes without saying that you need a client-side validation script as well.

Well, on re-readingyour original post, I guess you didn't mean quite what I thought you said about form submissions -- but I still stand by what I said. :)

8:04 pm on July 1, 2002 (gmt 0)


WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
votes: 93

I thought a while back someone mentioned that this fails in some browsers (let me guess, NS 4.x?) because they give precedence to the link, not the onclick event, so that even if the browser has JS on, it will still follow the href link, rather than the link in the onclick event.

Does that ring any bells?