Get current page URL and title with jQuery

23.Sep.2010

It could be useful to get current page URL and current page title. Especially if we want to make sharing plugin for our website. Often, we need that information in order to pass them as a parameter to sharing services like Twitter, Delicious, Facebook, etc…

So, in order to get these information, you can use backend language or JavaScript. In this demo, we shall be using jQuery, and its ways to get current page URL and current page title. Just copy the snippet below in order to use it on your website.

1
2
3
4
5
6
7
8
9
10
11
<p>Current page URL: <span id="this_url">www.example.com</span>.</p>
<p>Current page title: <span id="this_title">mytitle</span>.</p>
 
<script type="text/javascript">
jQuery(document).ready(function () {
	var href = jQuery(location).attr('href');
	var url = jQuery(this).attr('title');
	jQuery('#this_title').html('<strong>' + href + '</strong>');
	jQuery('#this_url').html('<strong>' + url + '</strong>');
});
</script>

Example of getting current page URL and title with jQuery

Current page URL: www.example.com.

Current page title: mytitle.