7

Possible Duplicate:
What does “javascript:void(0)” mean?

I want to ask a few questions regarding javascript:void(0)

<input type='submit' name='btn' value='submit' onClick='javascript:void(0)' />

Can you please explain void(0) - is it a built-in function? Does the keyword javascript represent that the code is written in javascript? If there is anything weird that you know about it, please share it with me. Thank you.

Kobe
  • 6,226
  • 1
  • 14
  • 35
Adeel Akram
  • 390
  • 2
  • 4
  • 12
  • 2
    Note that in an `on{someEvent}` attribute the `javascript:` protocol prefix is redundant. – scunliffe Oct 23 '12 at 18:48
  • 5
    [`void()`. at MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void). – David Thomas Oct 23 '12 at 18:49
  • 1
    As an event handler, I'd consider the `javascript:` prefix as invalid (though it might get executed as a labeled statement, which is not what the author wants) – Bergi Oct 23 '12 at 18:54
  • So many questions relating to `javascript:` and `void` under the **Related** section on the right side of this page, that I just don't know which one to pick. – I Hate Lazy Oct 23 '12 at 18:56

4 Answers4

7

void():

This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.

The void operator is often used merely to obtain the undefined primitive value, usually using "void(0)" (which is equivalent to "void 0"). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value). Note, however, that the javascript: pseudo protocol is discouraged over other alternatives, such as unobtrusive event handlers.

You can read more on this similar thread: What does "javascript:void(0)" mean?

Community
  • 1
  • 1
Chase
  • 29,019
  • 1
  • 49
  • 48
  • Yes, but you didn't include the important point of what that method is actually doing in this case (unless you click through your link) (still +1 though) – scottheckel Oct 23 '12 at 18:56
  • @Hexxagonal - I didn't want to post the whole content of what was on MDN, but if you think that it's beneficial to put here as well then I most certainly will =). – Chase Oct 23 '12 at 18:58
4

javascript:void(0) can be considered as "Do nothing". Not sure what was intended to be achieved with it here. If you wanted to prevent form submission on button click, you should have used something like

<input type='submit' value='submit' onClick='return false;' />
3

void is an operator that is used to return a undefined value so the browser will not be able to load a new page. An important thing to note about the void operator is that it requires a value and cannot be used by itself.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

It's defining an event handling function that has no body, so nothing gets executed. You will most commonly see it used in the context of an href attribute.

Maess
  • 4,118
  • 20
  • 29