Just in Chronicles

Life as a Voyage

How to Get URL Hash Programmatically with Javascript

leave a comment »

URL hash is not part of official URL fragments (RFC 2396), but is widely used for page navigation purposes. As this is not a part of web standards, even though most web browsers support this, there is no way to pass this value to server-side scripts, unless Javascript helps.

<script type="text/javascript">
	(function ($) {
		$(document).ready(function () {
			//	Appends the URL hash, if exists.
			var hash = window.location.hash;
			$("div.pagination a").each(function (i) {
				//var href = $(this).attr("href") + hash;
				$(this).attr("href", $(this).attr("href") + hash);
			});	//	$.each
		}); //	$.ready

Let’s see the Javascript code above. The highlighted line is the key to get the hash URL. With this approach, we can easily get the hash URL and append it to any anchor tag programmatically.

Written by Justin Yoo

08/03/2012 at 17:06

Posted in For Web

Tagged with ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.