Thursday, October 16, 2008

HTML Anchor Tag (<a> Link) with NO Redirection

Although this Blog is purely for Visual Basic Windows Forms Application Development. But I think we can discuss other stuff too.

So meanwhile working with HTML, I was trying to create an Anchor with a href defined but the link should NOT work. Click the following link,

Google

As you can see the above link should redirect this page to Google, but it is NOT redirecting it. OK now try to open the link in a new Tab (if you are using a borwser which has tabbed browsing support, i.e. Ctrl+Click). This time it should work. Here is the HTML,
<a href="http://www.google.com" onclick="return false;">Google</a>

As you can see in above "onclick" event we have executed a little javascript which returns false. This is the statement which is suppressing the default behaviour of anchor. Some sites handle it like this,

Google
<a href="javascript:void(0);" onclick="alert('You Clicked the Link');">Google</a>

No comments: