0

I have this cookie jQuery.cookie("previousUrl", window.location.href, {path:"/"}); to store the previous page url. How can I use this cookie as a window.location so that when a user clicks a "back button" it will take them to the page url set in the cookie? Cheers, Mike

user1813098
  • 25
  • 1
  • 7

2 Answers2

0

First get cookie value

var cookieVal = $.cookie('previousUrl');

then do

 window.location.href = cookieVal;
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • Thanks @chandresh_cool. This works pertaining to my question. For some reason though when the user clicks on a link the previousURL cookie stores the active link url and not the current page url. Any ideas on that one? – user1813098 Apr 09 '13 at 11:07
  • That is because your this function jQuery.cookie("previousUrl", window.location.href, {path:"/"}); stores the redirected url or the clicked url. – chandresh_cool Apr 09 '13 at 11:13
0

In a jQuery try this :

$(window).unload( function () 
{
   // read cookies value and if its set then window.location.href = cookie value
}

In javascript :

window.onbeforeunload = function () {
   // read cookies value and if its set then window.location.href = cookie value
}
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90