window.location redirect works, but original URL not showing up in browser history

user1546779 picture user1546779 · Jul 23, 2012 · Viewed 10.8k times · Source

The code below works well. Here is my problem: The window url redirects, but the original url is not logged in my browser history.

For example, if I visit "http://example.com/page1", the browser redirects to "http://example.com/test", 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.

Is there anyway to get the original url visited to log in my browser's history before redirecting?

<!-- script to enable age verification cookies and ensure people have age checked -->
<script type="text/javascript">
    $(document).ready(function(){
       if (window.location =="http://example.com/home") {//do nothing
       } else {
           window.location = "http://example.com/test";
       }
    });
</script>

Answer

Silver Gonzales picture Silver Gonzales · Jul 24, 2012

I think what you need is window.location.href. This adds the previous URL to the browser history.