Thursday, July 14, 2016

Get previous page URL using jQuery





We can get a previous page URL in jquery using a single property 'referer'.This is document object property.It returns the document URL of the previous page that loaded the current page.
Check the code below

$(document).ready(function(){
   var referer=document.referrer;
});

This is supported by all major browsers.But there is a small problem with the property here.It is quite possible that Page URL is shared through social media, email or accessed using direct link. So in that case, there is no referrer.

$(document).ready(function() {
  if(document.referrer.indexOf(window.location.hostname) != -1){
     var referrer =  document.referrer;
  }
});
 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More