Possible Duplicate:
what is the point of void in javascript
What is purpose of using void
here ? if just remove void()
, it should also work, right?
var b=document.body;
if(b&&!document.xmlVersion) {
void(z=document.createElement('script'));
void(z.src='http://www.google.ca/reader/ui/subscribe-bookmarklet.js');
void(b.appendChild(z));
}
else {
location='http://www.google.com/reader/view/feed/'+encodeURIComponent(location.href);
}
When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters.
In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters.
Why do we use a void pointer in C programs? We use the void pointers to overcome the issue of assigning separate values to different data types in a program. The pointer to void can be used in generic functions in C because it is capable of pointing to any data type.
The void functions are called void because they do not return anything. “A void function cannot return anything” this statement is not always true. From a void function, we cannot return any values, but we can return something other than values.
void
is a keyword that runs an expression and returns undefined
void 0 === undefined
void (foo = 42) === undefined
As to how href="javascript:..."
work.
unless the returned value is undefined.
You need to return undefined
or else the page is overwritten. Using the void
keyword is the easiest way to achieve this.
Yes. it will work without void.
it isn't necessary to use void there.
I googled your code snippet and it looks like its typically embedded in a link with "javascript:" in front of it. To quote the Mozilla reference for the void operator:
JavaScript URIs
When a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void operator can be used to return undefined. For example:
<a href="javascript:void(0);">Click here to do nothing</a> <a href="javascript:void(document.body.style.backgroundColor='green');">Click here for green background</a>
Note, however, that javascript: URIs are now often discouraged over other alternatives, such as events.
source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void
So it keeps the contents of the page from being overwritten when the code is executed inside of a link.
In this case, if the code is executed without the javascript: URI, the void operator should not make any difference. The void operator simply evaluates its input expression and returns undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With