Javascript Onclick Hyperlinks and Search Engines

by brad 28. January 2009 15:56

JavaScript onclick navigation is great for firing off special happenings when users click on hyperlinks.  It can be used for "Save your work before leaving" confirmations, "Are you sure?" questions, and "You're leaving our site" popups.  Not all of those are the best user experience but nevertheless they're quite common.

The most obvious way to accomplish this is with this HTML and JavaScript.

<a href="" onclick="navigate('mypage.html')">Click here</a>

In this case, the navigate method is called with the page to navigate to, and it can do whatever it likes before navigating there. This works great ... until a search engine or other crawler hits the site. It has no idea where this hyperlink goes, since it has no way of interpreting your 'navigate' method.

Better:

<a href="mypage.html" onclick="navigate(this.href)">Click here</a>

The navigate method can remain as-is and still get the URL to navigate to. And now a search engine can correctly interpret they hyperlink and make its way around your site.  Everyone's happy.

Tags:

Comments are closed

About Brad

Brad Tutterow lives in Illinois and works in Missouri. He has 12 years of experience developing web sites and Windows applications using a variety of technologies and is most excited currently about Silverlight, Windows Phone 7, Halo Reach, and Visual Studio 2010.