homepage Welcome to WebmasterWorld Guest from 54.227.40.166
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript links and submit buttons
A trick - for non javascript browsers
ggrot




msg:1488067
 12:46 am on Jun 28, 2002 (gmt 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.

 

rewboss




msg:1488068
 7:03 am on Jun 28, 2002 (gmt 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.

<edit>
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. :)
</edit>

ergophobe




msg:1488069
 8:04 pm on Jul 1, 2002 (gmt 0)

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?

Tom

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved