I've seen some people using void
operator in their code. I have also seen this in href
attributes: javascript:void(0)
which doesn't seem any better than javascript:;
我见过一些人在代码中使用void运算符。我也在href属性中看到了这个:javascript:void(0)它似乎没有比javascript更好:;
So, what is the justification of using the void
operator?
那么,使用void运算符的理由是什么?
3 个解决方案
#1
47
Explanation of its use in links:
其在链接中的使用说明:
This is the reason that bookmarklets often wrap the code inside void() or an anonymous function that doesn't return anything to stop the browser from trying to display the result of executing the bookmarklet. For example:
这就是bookmarklet经常将代码包装在void()内部或匿名函数中的原因,该函数不返回任何内容以阻止浏览器尝试显示执行bookmarklet的结果。例如:
javascript:void(window.open("dom_spy.html"))
If you directly use code that returns something (a new window instance in this case), the browser will end up displaying that:
如果您直接使用返回某些内容的代码(在这种情况下是一个新的窗口实例),浏览器将最终显示:
javascript:window.open("dom_spy.html");
In Firefox the above will display:
在Firefox中,上面将显示:
[object Window]
#2
11
Consider the following:
考虑以下:
<a href="javascript:void(fish=document.getElementById('foo').value);void(document.getElementById('bar').value=fish);">With Void</a>
<a href="javascript:fish=document.getElementById('foo').value;document.getElementById('bar').value=fish;">Without Void</a>
<input type="text" id="foo" value="one fish" />
<input type="text" id="bar" value="no fish" />
The first link will swap the values of the text fields. The second link will open a new page with the text "one fish". If you use a javascript: link
, the minute an expression returns something other than null
or undefined
, the browser will interpret that as what the link should do. By wrapping all expressions/statments in a void()
function, you ensure your entire snippet of code will run. These days, this is primarily of use in Bookmarklets, as using an onclick
attribute, or setting up event handlers in separate Javascript blocks/files is the "norm".
第一个链接将交换文本字段的值。第二个链接将打开一个带有“one fish”文本的新页面。如果使用javascript:link,表达式返回null或undefined之外的其他内容,浏览器会将其解释为链接应该执行的操作。通过将所有表达式/语句包装在void()函数中,可以确保运行整个代码片段。目前,这主要用于Bookmarklets,因为使用onclick属性,或者在单独的Javascript块/文件中设置事件处理程序是“常态”。
As for javascript:
vs. javascript:void()
, the first statement is ambiguous. You're saying, "hey, I want to run some javascript", but then you don't provide any code. It's not necessarily clear what the browser should do here. With the second statement you're saying "hey, run some javascript", and your code eventually returns undefined, which the browser knows means "do nothing".
至于javascript:与javascript:void(),第一个语句是不明确的。你说,“嘿,我想运行一些javascript”,但是你没有提供任何代码。浏览器应该在这里做什么并不一定清楚。使用第二个语句,你说“嘿,运行一些javascript”,你的代码最终返回undefined,浏览器知道这意味着“什么也不做”。
Since I'm here, I'll also point out that using either javascript:
or javascript:void();
has fallen out of favor with most people who care about markup. The better thing to do is have your onclick handler return false, and have the link pointed towards a page/resource that makes sense for people who have javascript turned off, or are using a javascript blocker such as NoScript.
既然我在这里,我也会指出使用javascript:或javascript:void();大多数关心加价的人已经失宠了。更好的办法是让你的onclick处理程序返回false,并将链接指向一个页面/资源,这对于关闭了javascript或使用NoScript等javascript阻止程序的人来说是有意义的。
#3
9
The undefined
value was not directly accessible in JavaScript until ES1.3.
在ES1.3之前,未在JavaScript中直接访问未定义的值。
An operator void <expression>
was therefore included to permit access to this value.
因此包含运算符void
It is sometimes useful, particularly when working with the Web API (e.g. event handlers), to ensure that the result of an expression is consistently undefined
.
它有时很有用,特别是在使用Web API(例如事件处理程序)时,以确保表达式的结果始终未定义。
When the undefined
property was added to the global object in ES1.3 the utility of void
became non-obvious.
当未定义的属性被添加到ES1.3中的全局对象时,void的实用程序变得不明显。
Hence your question.
因此你的问题。
#1
47
Explanation of its use in links:
其在链接中的使用说明:
This is the reason that bookmarklets often wrap the code inside void() or an anonymous function that doesn't return anything to stop the browser from trying to display the result of executing the bookmarklet. For example:
这就是bookmarklet经常将代码包装在void()内部或匿名函数中的原因,该函数不返回任何内容以阻止浏览器尝试显示执行bookmarklet的结果。例如:
javascript:void(window.open("dom_spy.html"))
If you directly use code that returns something (a new window instance in this case), the browser will end up displaying that:
如果您直接使用返回某些内容的代码(在这种情况下是一个新的窗口实例),浏览器将最终显示:
javascript:window.open("dom_spy.html");
In Firefox the above will display:
在Firefox中,上面将显示:
[object Window]
#2
11
Consider the following:
考虑以下:
<a href="javascript:void(fish=document.getElementById('foo').value);void(document.getElementById('bar').value=fish);">With Void</a>
<a href="javascript:fish=document.getElementById('foo').value;document.getElementById('bar').value=fish;">Without Void</a>
<input type="text" id="foo" value="one fish" />
<input type="text" id="bar" value="no fish" />
The first link will swap the values of the text fields. The second link will open a new page with the text "one fish". If you use a javascript: link
, the minute an expression returns something other than null
or undefined
, the browser will interpret that as what the link should do. By wrapping all expressions/statments in a void()
function, you ensure your entire snippet of code will run. These days, this is primarily of use in Bookmarklets, as using an onclick
attribute, or setting up event handlers in separate Javascript blocks/files is the "norm".
第一个链接将交换文本字段的值。第二个链接将打开一个带有“one fish”文本的新页面。如果使用javascript:link,表达式返回null或undefined之外的其他内容,浏览器会将其解释为链接应该执行的操作。通过将所有表达式/语句包装在void()函数中,可以确保运行整个代码片段。目前,这主要用于Bookmarklets,因为使用onclick属性,或者在单独的Javascript块/文件中设置事件处理程序是“常态”。
As for javascript:
vs. javascript:void()
, the first statement is ambiguous. You're saying, "hey, I want to run some javascript", but then you don't provide any code. It's not necessarily clear what the browser should do here. With the second statement you're saying "hey, run some javascript", and your code eventually returns undefined, which the browser knows means "do nothing".
至于javascript:与javascript:void(),第一个语句是不明确的。你说,“嘿,我想运行一些javascript”,但是你没有提供任何代码。浏览器应该在这里做什么并不一定清楚。使用第二个语句,你说“嘿,运行一些javascript”,你的代码最终返回undefined,浏览器知道这意味着“什么也不做”。
Since I'm here, I'll also point out that using either javascript:
or javascript:void();
has fallen out of favor with most people who care about markup. The better thing to do is have your onclick handler return false, and have the link pointed towards a page/resource that makes sense for people who have javascript turned off, or are using a javascript blocker such as NoScript.
既然我在这里,我也会指出使用javascript:或javascript:void();大多数关心加价的人已经失宠了。更好的办法是让你的onclick处理程序返回false,并将链接指向一个页面/资源,这对于关闭了javascript或使用NoScript等javascript阻止程序的人来说是有意义的。
#3
9
The undefined
value was not directly accessible in JavaScript until ES1.3.
在ES1.3之前,未在JavaScript中直接访问未定义的值。
An operator void <expression>
was therefore included to permit access to this value.
因此包含运算符void
It is sometimes useful, particularly when working with the Web API (e.g. event handlers), to ensure that the result of an expression is consistently undefined
.
它有时很有用,特别是在使用Web API(例如事件处理程序)时,以确保表达式的结果始终未定义。
When the undefined
property was added to the global object in ES1.3 the utility of void
became non-obvious.
当未定义的属性被添加到ES1.3中的全局对象时,void的实用程序变得不明显。
Hence your question.
因此你的问题。