Hashemian Blog

Web Tools, Financial Markets, Technology

Thursday, May 05, 2005

JavaScript void function and html hyperlinks 

One of the techniques often used in html are hyperlinks that actually do not take the user to another page, but instead run a client-side javascript, dhtml.
The most common way of programming hyperlinks in html is done by using the A (anchor) tag. The following code snippet illustrates the ease of creating a hyperlink:

<a href="http://www.hashemian.com/">www.hashemian.com</a>

Since the A tag has been with us since the inception of the Web, most people have come to expect that clicking on them causes an action. And even the most novice of html programmers are familiar with the tag.

Things however can get more complicated if you need the hyperlink click do something other than redirecting the user to another page. For example, you want to pop up a message, populate a field, syntax-check the user's input, or other client-side tasks. You know, dynamic html (dhtml) stuff. Enter the javascript:void(0) function. I ran into this feature years ago while searching for something, and I've been a loyal user ever since. The javascript:void(0) function basically neuters the hyperlink while allowing you to carry out some dhtml activity within the onClick event, i.e.:

<a href="javascript:void(0)" onclick="alert('You clicked.')" >www.hashemian.com</a>

The void() function causes the browser to take no redirection action upon clicking. In most modern, javascript-literate browsers you can accomplish the same disarming task by adding a "return false;" at the end of the onClick event code, but you'd still need to provide something in for the href property, so might as well make it javascript:void(0).

The story of void() doesn't quite end here. Although javascript:void(0) is the most widely used form of the void() function, you can actually have it invoke a javascript function like so:

<a href="javascript:void(alert('You clicked.'))" >www.hashemian.com</a>

This obviates wiring up the onClick event altogether. As always, your mileage may vary depending on your browser, so test your pages thoroughly.
<JavaScript void function and html hyperlinks>

3 comments |

3 Comments:

By Anonymous randomGuy, at 9/11/06 10:02 PM  

Posted 6:16 6/5/2005
Thanx for the javascript counter man. its one of the best out there and also very simple.
Good work...

By Anonymous Anonymous, at 18/12/06 10:26 AM  

I second the above post. Well done.

By Anonymous Anonymous, at 1/5/08 12:54 PM  

Be careful with void(0) in Internet Explorer (that should come as no surprise)

In particular, binding an onclick event handler to an anchor tag in IE, that also has the void href, will prevent the bound click event from firing.

Post a Comment

This page is powered by Blogger. Isn't yours?

Links
  • Hashemian Blog Feeds
  • Add to Google
  • Read Hashemian.com/blog/ with Bloglines
  • Subscribe to Hashemian.com/blog/ with My Yahoo!
  • Technorati Profile
  • TMCnet.com
  • ARCHIVES
  • 09/01/2003 - 10/01/2003
  • 03/01/2004 - 04/01/2004
  • 04/01/2004 - 05/01/2004
  • 05/01/2004 - 06/01/2004
  • 06/01/2004 - 07/01/2004
  • 07/01/2004 - 08/01/2004
  • 08/01/2004 - 09/01/2004
  • 09/01/2004 - 10/01/2004
  • 10/01/2004 - 11/01/2004
  • 11/01/2004 - 12/01/2004
  • 12/01/2004 - 01/01/2005
  • 01/01/2005 - 02/01/2005
  • 02/01/2005 - 03/01/2005
  • 03/01/2005 - 04/01/2005
  • 04/01/2005 - 05/01/2005
  • 05/01/2005 - 06/01/2005
  • 06/01/2005 - 07/01/2005
  • 07/01/2005 - 08/01/2005
  • 08/01/2005 - 09/01/2005
  • 09/01/2005 - 10/01/2005
  • 10/01/2005 - 11/01/2005
  • 11/01/2005 - 12/01/2005
  • 12/01/2005 - 01/01/2006
  • 01/01/2006 - 02/01/2006
  • 02/01/2006 - 03/01/2006
  • 03/01/2006 - 04/01/2006
  • 04/01/2006 - 05/01/2006
  • 05/01/2006 - 06/01/2006
  • 06/01/2006 - 07/01/2006
  • 07/01/2006 - 08/01/2006
  • 08/01/2006 - 09/01/2006
  • 09/01/2006 - 10/01/2006
  • 10/01/2006 - 11/01/2006
  • 11/01/2006 - 12/01/2006
  • 12/01/2006 - 01/01/2007
  • 01/01/2007 - 02/01/2007
  • 02/01/2007 - 03/01/2007
  • 03/01/2007 - 04/01/2007
  • 04/01/2007 - 05/01/2007
  • 05/01/2007 - 06/01/2007
  • 06/01/2007 - 07/01/2007
  • 07/01/2007 - 08/01/2007
  • 08/01/2007 - 09/01/2007
  • 09/01/2007 - 10/01/2007
  • 10/01/2007 - 11/01/2007
  • 11/01/2007 - 12/01/2007
  • 12/01/2007 - 01/01/2008
  • 01/01/2008 - 02/01/2008
  • 02/01/2008 - 03/01/2008
  • 03/01/2008 - 04/01/2008
  • 04/01/2008 - 05/01/2008

  • Read Financial Markets  |   Home  |   Blog  |   Web Tools  |   News  |   Articles  |   FAQ  |   About  |   Contact

    © 2001-2009 Robert Vahid Hashemian
    Support the effort
    Liked this page?
    Please consider creating a link to it
    from your Web site.

    hashemian.com
    هاشمیان.com

     Home

     Blog

     Web Tools Add Free Web Tools custom Google Toolbar button (Requires Toolbar >V4)
    Usage

     News

     Articles

     FAQ

     About

     Contact

     Financial Markets Book
    Read Complete Book



    BOOK
    Head First Design Patterns
    Kathy Sierra
    $44.95


    BOOK
    JavaScript: The Good Parts
    Crockford Douglas
    $29.99


    BOOK
    Learning jQuery 1.3
    Karl Swedberg
    $39.99


    BOOK
    JavaScript: The Definitive Guide
    Flanagan David
    $49.99


    BOOK
    Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)
    Nixon Robin
    $40.00

    |javascript-void-function-and-html|

    more…




    Get Kindle, $259

    aStore - Hashemian.com on Amazon

    Visits: Powered by hashemian.com

     

     

     

     

     

    Search Hashemian.com





    TUNNEL MTCE MAN MINIATURE W BROOM AND LANTERN 1/24 G
    $6.49
    Ends: Wed Nov 25, 2009 21:48:07 EST


    Brand New Playmobil 5828 Black and Green Dragon Knight
    $0.99
    Ends: Wed Nov 25, 2009 21:48:11 EST


    vintage lot (11) plain and embroidered tea towels
    $9.99
    Ends: Wed Nov 25, 2009 21:48:18 EST


    Better Homes And Gardens Drying Your Own flowers
    $4.11
    Ends: Wed Nov 25, 2009 21:48:27 EST


    LEGO 8050 EXPERT BUILDER SET ORIGINAL BOX AND INSTRUCTI
    $9.99
    Ends: Wed Nov 25, 2009 21:48:30 EST

    more…