3 ms·
This may sound like a stupid question, but if you don't want the browser to follow a link when somebody clicks it, why not just leave off the href attribute?
by qntm 16y ago
This may sound like a stupid question, but if you don't want the browser to follow a link when somebody clicks it, why not just leave off the href attribute?
- JonnieCache 16y agoWell, firstly a link tag with an empty href is semantically meaningless, and it probably doesn't validate either. More importantly this is part of a common pattern for progressive enhancement. You create all your links with normal href attributes that point to real pages. If the browser doesn't have javascript enabled (eg. the googlebot) then navigation works as normal. Then to enable your ajax navigation, you add an event handler to the links that pulls the href attribute out and retrieves the content at that url via ajax. You have your server notice that the request is an ajax one and only send back the content without the layout, which the js then uses to update the page. Obviously the browser must be prevented from following the link in the normal way. Bingo, AJAX navigation without any extra markup, and with a flawless fallback.
- kragen 16y ago<a name=""> has been legal since before HTML had veersion numbers.
- ch0wn 16y agoAn <a> element does not behave like a link, if you leave the href attribute off. For example, the css rules for the :link pseudo-class do not apply. Also, in some cases it can be helpful, to have a no-js fallback.
- bzbarsky 16y agoFor one thing, because then someone trying to use your page with a keyboard instead of a mouse is screwed: anchors without href don't tend to be focusable by keyboard action in web browsers. You can't even hack around this with tabindex, at least in Gecko and WebKit, though those seem like browser bugs (Presto gets this right).