PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » JavaScript » “Go Back” Button

“Go Back” Button

Browsers already have "back" buttons, so you'd better have a darn good reason for needing to put one on your page!

Input button with inline JavaScript

<input type="button" value="Go Back From Whence You Came!" onclick="history.back(-1)" />

This is totally obtrusive, but you could fix that by only appending this button through JavaScript.

PHP

If JavaScript isn't a possibility, you could use the HTTP_REFERER, sanitize it, and echo it out via PHP.

<?php
  $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
  echo "<a href='$url'>back</a>";
?>

Reference URL

Subscribe to The Thread

  1. 1. Many users aren’t aware of their browser back button.
    2. Users filling forms often make errors. Therefore, they complete the form, it goes through edit checks on the server, then it displays their input and asks if it is correct. If not, they need to return to correct it. A return button, front-and-center, becomes applicable.
    3. The browser back button and the scripted back button do not always behave in the same manner. If you’re looking for a specific behavior, then, it needs to be created.

  2. Oh. The javascript:history.go(-1) and its derivatives returns an Expired Page in IE8

  3. simple HREF=”javascript:history.go(-1)”>Back

  4. Just wanna say thanks for the ‘Back’ button/link… working perfectly :) I used the one in php with one of my projects.

    Thanks a bunch!

  5. hey
    thanks for php version, it works great.

    may be you can help me in customize it… i need conditional construct with check about count of previous pages. i want to hide this button if user looks his first page. sometimes it’s not the main page.

    tia.

  6. Been looking for this for ages, thanks . Will come in very useful at the bottom of an article ,but is there a way to change the button for a image.

  7. Ben Smalley

    David if you really need to ask that i’m not sure your in the right place, but here you are bud,

    Good luck!

  8. the php code works, but there’s an issue and I need some help fixing it.

    Example:
    I go to page A
    then I go to Page B, and click to go to Page C,
    When I click the back button on Page C, I get to Page B.
    When I click the back button on Page B, I get to Page C.

    How can I fix this?

  9. I love this button its had great use on my site, thank you for taking your time to share :).

  10. Other than what was said in the first post, is there any reason for it to be like that?

  11. Charlie

    I reach a page with an update form on it which uses Dreamweavers PHP/MySQL Update Record function. I come to this from vatious source pages and want to go back to the page that sent me there once I hit the submit button. Given all the PHP code it is not a simple matter of chaning or creating a button with a link on it.

    Any ideas on having the submit button go back to whatever page send it there in the first place?

  12. I’ve got a new BibleRhymes site in the final stages of creation and I think your JS code will be a part of it. Thank youu!

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~