1

The code I've included below is meant to redirect to a new url if the user is not on a specific page or does not have a certain cookie. The cookie function works perfectly as does the redirect. Here is my problem: The window url redirects, but the original url is not logged in my browser history.

<script type="text/javascript">
    $(document).ready(function() {
        if (getCookie('legal_age') == "yes" || window.location =="http://example.com/home") {//user is legal age!
        } else {
            setTimeout(function() {
                window.open('http://example.com/welcome','_self','', false);
            },0)
        }
    });
</script>

For example, if I visit "http://example.com/page1", the browser redirects to "http://example.com/welcome", as it should. However, I need the original url visited ("http://example.com/page1") to show up in my browser history so that I can call upon it in a different function. Here is the code I am using to call the history (within a form):

<form action="javascript:window.location.reload(history.go(-1));" method="get" name="age_form" id="ageForm" />

I've also tried this alternative to call the history and it didn't help:

window.history.back();

I have also tried the following with no success in saving original url in browser history:

window.location = "http://example.com/welcome";
window.location.href = "http://example.com/welcome";
window.location.assign("http://example.com/welcome");

Finally, I included this function because another thread suggested it might help, but it hasn't seemed to do much:

setTimeout(function(){

Any ideas? Is there anyway to get the original url visited to log in my browser's history before redirecting? HELP please!

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
user1546779
  • 121
  • 1
  • 2
  • 6
  • this may help: http://stackoverflow.com/questions/5018698/js-redirecting-with-window-location-not-saving-history-in-firefox – ahren Jul 24 '12 at 01:29
  • @ahren i had seen that thread, but the solution to that was to use the `setTimeout(function(){ ` which didn't seem to solve my problem. – user1546779 Jul 24 '12 at 01:34
  • @MrOBrian here is a better explanation of my question and the things I have tried. Any ideas at all? I'm really stumped with this one... – user1546779 Jul 24 '12 at 02:57
  • what's odd is that sometimes the original URL does show up in the history bar on my browser, but when I call for `window.history.back();` or something similar, it always redirects to the page I was on prior to entering the original URL (i.e. two pages previous) – user1546779 Jul 24 '12 at 02:59
  • I believe this is because `window` knows it has been redirected, and by using `.back()` it would know that it could be stuck in an infinite 'redirection' loop. – ahren Jul 24 '12 at 03:26
  • Why not store the current page location in a cookie before redirecting? Then read the cookie for your different function. – Sablefoste Jul 24 '12 at 03:30
  • @SableFoste I think a cookie might work! Great idea. I'll give it a shot...thanks so much! – user1546779 Jul 24 '12 at 04:01

1 Answers1

0

setting a cookie of the current URL before redirecting worked well. Here is the altered code that worked. keep in mind that in order to set a cookie, you must reference a javascript cookie function (in this case mine is called setCookie():

<script type="text/javascript">
    $(document).ready(function(){
       if (getCookie('legal_age') == "yes" || window.location =="http://example.com/home") {//user is legal age!
       } else {
           setCookie('originalURL', window.location);
           var urlCheck = getCookie('originalURL');
           window.open('http://example.com/welcome','_self','', false);
       }
    });
</script>

Hope this helps someone!

Rob
  • 4,927
  • 12
  • 49
  • 54
user1546779
  • 121
  • 1
  • 2
  • 6