Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an empty javascript link mean?

Tags:

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?

like image 592
bege Avatar asked Oct 17 '25 03:10

bege


2 Answers

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)

like image 172
T.J. Crowder Avatar answered Oct 18 '25 15:10

T.J. Crowder


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.

like image 23
bdukes Avatar answered Oct 18 '25 17:10

bdukes