Amy
Amy

Reputation:

Go back a page onload?

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

Upvotes: 3

Views: 8724

Answers (5)

Michele DC
Michele DC

Reputation: 109

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)">

Upvotes: 1

Oliver Leuyim Angel
Oliver Leuyim Angel

Reputation: 74

well another solution is:

<body onload='history.go(-1);'>

Upvotes: 0

Joe Chung
Joe Chung

Reputation: 12123

$(function() { history.go(-1); });

Upvotes: 1

Frankie
Frankie

Reputation: 25165

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.

Upvotes: 4

Jed Smith
Jed Smith

Reputation: 15944

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.

Upvotes: 4

Related Questions