Here'S What Javascript:void(0) Actually Means

The void operator is often used merely lớn obtain the undefined primitive sầu value, usually using “void(0)” (which is equivalent lớn “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to lớn a non-default value).

Bạn đang xem: Here's what javascript:void(0) actually means

An explanation is provided here: void operator.

The reason you’d want to vày this with the href of a links 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 to undefined.


*

*

In addition to 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’, và so on. This happens quite a lot now people have got used to lớn middle-click-for-new-tab: it looks like a links, you want lớn read it in a new tab, but it turns out khổng lồ be not a real links at all, & gives unwanted results lượt thích a blank page or a JS error when middle-clicked.

Xem thêm: Download Ghost Win 10 32Bit, Download Ghost Win 10 Pro 2018

is a comtháng alternative sầu which might arguably be less bad. However you must rethành viên to return false from your oncliông xã event handler to lớn prsự kiện the liên kết being followed & scrolling up lớn the top of the page.

In some cases there may be an actual useful place to lớn point the link to 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 to use to lớn liên kết to lớn it. Or if there is a non-JavaScript way of doing the same thing (for example, ‘thispage.php?show=foo’ that sets foo visible lớn begin with), you can liên kết khổng lồ that.

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

The disadvantage of this is that you thua thảm keyboard control, since you can"t tab onkhổng lồ 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 to 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 lớn the keyboard differently (not lớn mention non-visual browsers).

If you really want an element that isn"t a link 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 restyle it so it looks more lượt thích a link 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 lớn a location, but instead calls active sầu code inside the current location. They have caused massive security problems for both browsers and webapps, and should never have been invented by Netscape.)