The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent khổng lồ “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 void 0 do in javascript?

An explanation is provided here: void operator.

The reason you’d want to lớn bởi this with the href of a links is that normally, a javascript: URL will redirect the browser lớn 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 and simple script that evaluates khổng lồ undefined.



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

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

Xem thêm: Cách Lên Đồ Rakan Sp, Bảng Ngọc Và Khắc Chế Rakan, Rakan Mùa 11: Bảng Ngọc, Cách Lên Đồ Rakan

is a comtháng alternative sầu which might arguably be less bad. However you must remember to lớn return false from your oncliông chồng event handler to prsự kiện the link being followed and scrolling up to lớn the top of the page.

In some cases there may be an actual useful place to lớn point the liên kết lớn. For example if you have a control you can cliông chồng on that opens up a previously-hidden
, it makes some sense lớn use khổng lồ link khổng lồ 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 begin with), you can link khổng lồ that.

Otherwise, if a links points only lớn some script, it is not really a liên kết & should not be marked up as such. The usual approach would be to lớn add the oncliông xã to a ,
, or an without an href and style it in some way to lớn make it clear you can clichồng on it. This is what neftekumsk.com .

The disadvantage of this is that you thua thảm keyboard control, since you can"t tab onto lớn a span/div/bare-a or activate it with space. Whether this is actually a disadvantage depends on what sort of action the element is intended lớn take. You can, with some effort, attempt lớn mimic the keyboard interactability by adding a tabIndex to the element, and listening for a Space keypress. But it"s never going khổng lồ 100% reproduce the real browser behaviour, not least because different browsers can respond to the keyboard differently (not lớn mention non-visual browsers).

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 like a links than a button, if you want. But since it behaves like 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 sầu caused massive security problems for both browsers and webapps, và should never have sầu been invented by Netscape.)