View Full Version : javascript back button

05-28-2006, 03:46 PM
I am doing a javascript back button within my form

<input type=submit value="Back to previous page" class=header onClick="history.go(-1)">

for some reason it is submitting the form instead of going back a page --

why would this be?

Bill Posters
05-28-2006, 04:11 PM
onClick="history.go(-1); return false;"

Perhaps you should consider not using a submit button in the first place.

<input type="button" onClick="history.go(-1); return false;">

Bear in mind that this isn't going to work if the user doesn't have js enabled/available.
Ideally, you'd make the presence of this button dependant on js, either by using js to inset it into the page or by only having this button appear beyond some js-only threshold.

Alternatively, you could consider using PHP to dynamically code the url into a proper link based on the referrer.


<p><?php if (isset($_SERVER['HTTP_REFERER'])) echo '<a href="'.$_SERVER['HTTP_REFERER'].'">Back</a>'; ?></p>

Davide Zanotti
05-28-2006, 04:32 PM
try to use: history.back()

05-28-2006, 11:57 PM
David, the issue wasn't the JavaScript itself, but rather the fact that the input type was submit rather than button. They are quite different in that the form is sent to it's defined or undefined referrer onclick of type=submit.