Forum Moderators: open

Message Too Old, No Replies

href="javascript:" or onclick?

         

benj0323

8:41 pm on Jul 25, 2004 (gmt 0)

10+ Year Member



I've been wondering this for a while, but which way is the preferred way to go?

href="javascript:some_function()"

or

onclick="some_function()"

Which way is more professional? Are there downsides to using one of those methods?

Thanks a lot for you help.

Rambo Tribble

10:29 pm on Jul 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While there seem to be those with a strong distaste for the javascript: pseudo-URL, in many cases it can serve as well as onclick.

A big difference is that the pseudo-URL can only be used as the target of a link, while onclick can be used with almost any element, including block elements.

If you have a link, the only purpose of which is to fire a script, the outcome of using the pseudo-URL is little different than the onclick. Bear in mind, if you use the onclick on such a link you will need to set the href of the link to something benign, like href="#", otherwise strange things can happen if JavaScript is disabled.

A common and useful trick is to point the link to a fall-back URL for a page that displays if JS is disabled. The onclick action then returns "false" to cancel the link's activation, if the script runs successfully. Searching this forum you will be able to find many examples of this kind of usage.

encyclo

1:06 am on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As Rambo Tribble says, the
onclick
method is far better than the (deprecated)
javascript:
pseudo-URL, as the former allows you to have a fallback solution if the user has Javascript disabled. The very simplest example is with a link which opens a popup window - if you use
onclick
, you can offer the same page in a new window for users without Javascript:

<a href="page.html" target="_blank" onclick="popup('variables');return false;">Click click</a>

One popular use of the

javascript:
pseudo-URL is offering a "previous page" option - when you don't know what the previous page is the person has come for. For example:

<a href="javascript:history.go(-1)">Back</a>

In this case, it would be better in usability terms to write the link out with Javascript document.write, and tell the user to click the back button if they have Javascript disabled - something vaguely like:

<script type="text/javascript">document.write('<a href="javascript:history.go(-1)">Click here to go to the previous page</a> or')</script> Click the "back" button.

That way, users without Javascript don't get a broken (for them) link.

benj0323

2:27 am on Jul 26, 2004 (gmt 0)

10+ Year Member



When I first started developing I took into account those that didnt have javascript enabled, but today is that really a problem? I've yet to see a computer that has it disabled.

Thank you for your replies, it helped a ton.

Rambo Tribble

3:55 am on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With the exploits that have arisen using JScript to manipulate ActiveX and other techniques using JavaScript, more and more admins are turning JS off on client machines.

encyclo, I'm confused by your typification of the pseudo-URL as "deprecated". The p-U is a browser feature, not a language or markup specification. Who's doing the deprecation?