...

View Full Version : Change the page refresh URL



tomharto
01-17-2012, 12:51 PM
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)

mjy
01-17-2012, 03:48 PM
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.


<head>
<title>JavaScript Tester Page</title>

<script>

window.onbeforeunload = function ()
{
return "Are you sure you want to return to the home page?";
}

</script>

</head>

tomharto
01-17-2012, 03:56 PM
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?

mjy
01-18-2012, 04:13 PM
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.

felgall
01-18-2012, 08:44 PM
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.

mjy
01-19-2012, 03:56 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum