views:

286

answers:

3

Is there a way for a page to go back a page onload using javascript or php?

+2  A: 

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.

Jed Smith
So would it be something like this?<html><body onload="history.go(-1)"></body></html>
That's correct.
Jed Smith
I just tried it, it doesn' go back when the page loads.
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
+2  A: 

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"&gt;

Hope it helps! Be aware that this behavior is, generally, confusing to the user and should be used with caution.

Frankie
That's mootools not jquery, just fyi
rpflo
Sure it is. Just like it says right before the code: "Javascript mootools example"... but thanks for pointing it out, I guess! :)
Frankie
A: 
$(function() { history.go(-1); });
Joe Chung