Get current page URL and title with jQuery


/ Published in: JavaScript
Save to your folder(s)



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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.