Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Change the page refresh URL

    Is it possible to change the URL when a page refreshes?

    E.G Someone clicks a link on index.php to page1.php.
    When you refresh page1.php it actually loads page2.php

    The reason i ask my site uses AJAX to change pages (therefore doesnt refresh and stays on the index.php page). So if someone has followed 3-4 links and refreshes the page it will go back to the homepage, i want it so it reloads the currently page (i can use query strings to get the relevant page)

  • #2
    mjy
    mjy is offline
    New Coder
    Join Date
    Jan 2012
    Location
    United States
    Posts
    28
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I haven't thought this through all the way, but one thing you might experiment with is a window.onbeforeunload event handler, which gets called when the user navigates away from the window and ALSO when the user simply refreshes the window with F5 or whatever. One possibility: If the handler returns a string, the browser lets the user block the refresh and stay on the current page.
    Code:
    <head>
    <title>JavaScript Tester Page</title>
    
    <script>
    
    window.onbeforeunload = function ()
       {
       return "Are you sure you want to return to the home page?";
       }
    
    </script>
    
    </head>

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    That brought up a message before the page refreshed , so if all else fails that will work, i tried putting there window.location.href = 'testPage'; however it still refreshed the page, anyway i can stop the refresh altogether and just make it follow that link instead?

  • #4
    mjy
    mjy is offline
    New Coder
    Join Date
    Jan 2012
    Location
    United States
    Posts
    28
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I also tried setting window.location.href (and window.location), but for some reason the setting is ignored at that point. I don't know of any way to forcibly stop a refresh, but I think if you return a good message from the onbeforeunload handler explaining the "Leave this page" and "Stay on this page" options, the user will be able to make the right choice.

    Something else you might experiment with (although it could be a little complex and awkward) is in the onbeforeunload handler, somehow saving the user's page state in a server database and then restoring it when the script reloads. Just a thought -- I might not be understanding exactly what you're doing here.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The way to do it would be to set a cookie when the person first visits the page. Prior to setting the cookie you test if it is already set and if so redirect to the other page.

    That would mean that the second time they try to access the page it will redirect. This will therefore work if they reload the page - but it will not work if they simply refresh the page as that doesn't actually reload the page and so the script doesn't get rerun.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    mjy
    mjy is offline
    New Coder
    Join Date
    Jan 2012
    Location
    United States
    Posts
    28
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Using a cookie to store the last location is a great idea. If you'd like me to send you example code for writing and reading cookies, taken from my book "JavaScript: Just the Basics," I'd be happy to. Just contact me privately.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •