tags:

views:

55

answers:

3

Hi,

I have a PHP page that has an error, but if I go back, it works fine. I can't redirect because the page can be accessed from two different places.

How can I make a JavaScript that loads automatically when the page loads?

Thanks, Sebastian

EDIT

that wouldn't work. is there a way to get the url of history.go(-2)?

thanks, Sebastian

A: 

You can use the window.history object, like this:

history.back();
//or:
history.go(-1);
Nick Craver
called directly in my php page? will this work?
sebastian
@sebastian are you asking if you can call this function in PHP? If so, No - No You can't
Hannes
Doesn't your PHP script generate plain HTML?
Álvaro G. Vicario
@sebastian - If it's in a script block, yes it'll work...I'm still unclear on your exact usage though, why not redirect to the referrer in PHP (with a header)?
Nick Craver
A: 
...
<body onload="history.go(-1);">
...
Vytautas
A: 

The problem with using the window's onload property is that you do not want the user to wait for the current page to load. Just put this code/HTML at the top of the page right after the opening <head> tag.

<script type="text/javascript">history.go(-1);</script>
Eric