Solved

javascript:void(0);

Posted on 2011-09-07
10
1,487 Views
Last Modified: 2012-05-12
Often when I hover over a link I see this in the browser link "hint" at the bottom:

javascript:void(0);


Why does it show this?  And it does it for several links, each one says the same thing.
0
Comment
Question by:knowlton
  • 4
  • 3
  • 3
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36496135
just mean do nothing : http://www.2ality.com/2011/05/void-operator.html

you ask to evaluate 0
void return nothing
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36496155
This prevents an actual <a href="" /> from firing it's href value. That means the page won't be refreshed, redirected, etc. It forces the javascript methods (i.e. onclick) to be fired.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36496182
0
 
LVL 5

Author Comment

by:knowlton
ID: 36496216
So you can have a bunch of different links get processed in one central function and decide what to do based on the source of the click, vs hard coding the href for each link?
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36496251
no, the void is placed like so:

<a href="javascript:void(0);" onclick="DoSomething()">Click Me</a>

The void(0); tells the browser to NOT evaluate the href attribute value. Instead, perform any javascript events on the link.

If you only had the following, it would do NOTHING when the link is cliked:

<a href="javascript:void(0);">Do Nothing</a>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 82

Expert Comment

by:leakim971
ID: 36496262
no that's not the purpose, people use # instead javascript:void(0)

the link stay a link (mouse over effect, underline, ...) but when you click on it nothing happen
most of time you have the onclick attribute set for the link

you can use : onclick="return false;" too
0
 
LVL 5

Author Comment

by:knowlton
ID: 36496318
So what might be the reason for doing this?  A practical application?

For example, when I visit an answered question on EE, at the top it has links to "share the solution" on facebook, twitter, etc. and as I hover over each button (each link) it says:

javascript:void(0);
0
 
LVL 28

Accepted Solution

by:
strickdd earned 250 total points
ID: 36496367
The most common use is so that a link that goes nowhere can be center-clicked to open a new tab/page.
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 36496373
as you know clicking on a link redirect the user to a new page
using javascript:void(0) or # you take your link in place and add a new logic on the click
when the user click on the link you can open a CSS popup, a modal window, doing ajax operation and so on

in the other case you change the location of the current page (or reload the page if it's the same URL)
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 36496383
thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sums of coloumns in html/java 15 67
Recordset containing single quotes 8 23
Change javascript css 1 20
using php variable inside javascript 5 15
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now