The void operator is often used merely to lớn 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 lớn a non-default value).

Bạn đang xem: What does javascript void(0) mean?

An explanation is provided here: void operator.

The reason you’d want to bởi vì this with the href of a link is that normally, a javascript: URL will redirect the browser khổng lồ a plain text version of the result of evaluating that JavaScript. But if the result is undefined, then the browser stays on the same page. Void(0) is just a short và simple script that evaluates khổng lồ undefined.



In addition to lớn the technical answer, javascript:void means the author is Doing It Wrong.

There is no good reason to use a javascript: pseudo-URL(*). In practice it will cause confusion or errors should anyone try things like ‘bookmark link’, ‘open liên kết in a new tab’, and so on. This happens quite a lot now people have got used to middle-click-for-new-tab: it looks lượt thích a link, you want khổng lồ read it in a new tab, but it turns out to be not a real links at all, & gives unwanted results lượt thích a blank page or a JS error when middle-clicked.

is a common alternative which might arguably be less bad. However you must remember khổng lồ return false from your onclick event handler to prevent the link being followed và scrolling up khổng lồ the đứng top of the page.

In some cases there may be an actual useful place to point the links to. For example if you have a control you can click on that opens up a previously-hidden
, it makes some sense lớn use to liên kết to it. Or if there is a non-JavaScript way of doing the same thing (for example, ‘thispage.php?show=foo’ that sets foo visible to lớn begin with), you can link to that.

Otherwise, if a liên kết points only lớn some script, it is not really a link and should not be marked up as such. The usual approach would be to địa chỉ cửa hàng the onclick to a ,
, or an without an href and style it in some way khổng lồ make it clear you can click on it. This is what .

The disadvantage of this is that you chiến bại keyboard control, since you can"t tab onto a span/div/bare-a or activate it with space. Whether this is actually a disadvantage depends on what sort of kích hoạt the element is intended to lớn take. You can, with some effort, attempt to mimic the keyboard interactability by adding a tabIndex to lớn the element, và listening for a Space keypress. But it"s never going to lớn 100% reproduce the real browser behaviour, not least because different browsers can respond to lớn the keyboard differently (not to lớn mention non-visual browsers).

Xem thêm: Kết Quả Xổ Số Miền Nam Ngày 27 Tháng 5 Năm 2020 Hôm Nay Thứ 4

If you really want an element that isn"t a links but which can be activated as normal by mouse or keyboard, what you want is a (or is just as good, for simple textual contents). You can always use CSS to lớn restyle it so it looks more lượt thích a liên kết than a button, if you want. But since it behaves lượt thích a button, that"s how really you should mark it up.

(*: in site authoring, anyway. Obviously they are useful for bookmarklets. Javascript: pseudo-URLs are a conceptual bizarreness: a locator that doesn"t point to lớn a location, but instead calls active code inside the current location. They have caused massive security problems for both browsers and webapps, & should never have been invented by Netscape.)