![]() The syntax for the void operator is: void «expr» which evaluates expr and returns undefined. To understand this concept one should first understand the void operator in JavaScript. They have caused massive security problems for both browsers and webapps, and should never have been invented by Netscape.) javascript: pseudo-URLs are a conceptual bizarreness: a locator that doesn't point to a location, but instead calls active code inside the current location. Obviously they are useful for bookmarklets. But since it behaves like a button, that's how really you should mark it up. You can always use CSS to restyle it so it looks more like a link than a button, if you want. 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). ![]() But it's never going to 100% reproduce the real browser behaviour, not least because different browsers can respond to the keyboard differently (not to mention non-visual browsers). You can, with some effort, attempt to mimic the keyboard interactability by adding a tabIndex to the element, and listening for a Space keypress. Whether this is actually a disadvantage depends on what sort of action the element is intended to take. The disadvantage of this is that you lose keyboard control, since you can't tab onto a span/div/bare-a or activate it with space. The usual approach would be to add the onclick to a, , or an without an href and style it in some way to make it clear you can click on it. ![]() Otherwise, if a link points only to some script, it is not really a link and should not be marked up as such. 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 to that. For example if you have a control you can click on that opens up a previously-hidden, it makes some sense to use to link to it. In some cases there may be an actual useful place to point the link to. However you must remember to return false from your onclick event handler to prevent the link being followed and scrolling up to the top of the page. is a common alternative which might arguably be less bad. ![]() This happens quite a lot now people have got used to middle-click-for-new-tab: it looks like a link, you want to read it in a new tab, but it turns out to be not a real link at all, and gives unwanted results like a blank page or a JS error when middle-clicked. In practice it will cause confusion or errors should anyone try things like ‘bookmark link’, ‘open link in a new tab’, and so on. ![]() There is no good reason to use a javascript: pseudo-URL(*). In addition to the technical answer, javascript:void means the author is Doing It Wrong. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |