开发者

What does an empty javascript link mean?

开发者 https://www.devze.com 2023-02-02 00:14 出处:网络
I inspected the inbox link in hotmail using firebug and saw something like that: <a ... href=\"javascript:; .... />

I inspected the inbox link in hotmail using firebug and saw something like that:

<a ... href="javascript:; .... /> 
开发者_高级运维

I just can't figure out how the postback is realizing when I click the link. And what does "javascript:;" mean while it doesn't refer to any function?


The javascript: part there is a pseudo-protocol meaning that the URI should be interpreted as JavaScript code. The ; immediately after it is a statement terminator. Assuming that nothing else follows, it basically makes the link do nothing when clicked.

If something is happening when the link is clicked, I'm guessing a click event handler has been attached to it or one of its ancestor elements. click bubbles up the DOM, so you don't have to watch it actually on the element itself.

You won't necessarily see those event handler attachments in the HTML; the page may well use unobtrusive techniques to hook up the handler later.

Gratuitous live example #1 (hooking the click on the link unobtrusively)

Gratuitous live example #2 (hooking the click on an ancestor element)


It's evaluating the expression ;, which doesn't do anything. It's just there so that there's something in the href (otherwise it won't behave like a link).

The actual behavior is being wired-up somewhere else. For example, it might be wireup up with something like this jQuery statement: $('#inboxLink').click(goToInboxFunction)

Or, as @T.J. Crowder points out, the click handler could be wired-up higher up in the DOM and use event bubbling the capture it for this link.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号