Is there a way for a page to go back a page onload using javascript or php?
Asked
Active
Viewed 8,377 times
3
-
1Less effort to just provide an answer to the specific question and avoid the argument over the reasoning, IMO. I could see an arcane use for this. – Jed Smith Oct 01 '09 at 02:49
5 Answers
4
Yes, use history.go(-1)
in Javascript.
For bonus points, ensure that there is actually a page to go back to before calling it, like this question suggests.
-
1
-
Probably the browser protecting you from doing it, or you have another issue at work. Use the Javascript debugger God and Safari provide you. The given answer is most certainly how to do it. – Jed Smith Oct 01 '09 at 02:50
4
PHP is server side, so no. The page will get to the client and, when it reaches the client, the browser has to be instructed to go back.
That said you have two options. Javascript or HTML Meta Tags.
Javascript mootools example would be something like this:
window.addEvent('domready', function() {
history.go(-1);
});
You can also send it in HTML Meta Tags
<meta http-equiv="refresh" content="2;url=http://example.net">
Hope it helps! Be aware that this behavior is, generally, confusing to the user and should be used with caution.

Frankie
- 24,627
- 10
- 79
- 121
-
-
2Sure it is. Just like it says right before the code: "Javascript mootools example"... but thanks for pointing it out, I guess! :) – Frankie Oct 01 '09 at 08:43
1
Sometimes Safari in iOS ignores history.go(-1).
So I prefer to use the window.history.back() method:
<body onload="window.history.back()">
You can also delay the back operation to show a temporary message in the html body:
<body onload="setTimeout(function() { window.history.back(); }, 1000)">

Michele DC
- 109
- 1
- 11