[Javascript] - Creating a "Go Back" Link.

By @thedevchick9/6/2017javascript

Sometimes you need a link or button on your page that will "Go Back" just like the Back button in the browser.

If you're having a hard time thinking of when you might need to do this, imagine you load data into another page with an iFrame (I know gross, but it happens from time to time). In that scenario, after taking some action within the iFrame you may want your users to be able to go back to complete the action or some other action again if they so desire. Clicking the Back button on the browser will do you no good in that scenario as the iFrame is what you want to go back, not the main page.

Problem Solved!

You can use Javascript and add an onclick event to a button, link, or really any element, and for the example above, simply put that link/button on the page in the iFrame.

Here's the code, very simple!

Screen Shot 2017-09-06 at 5.08.13 PM.png

<input type="button" 
       value="Go Back" 
       onclick="history.back(-1)"
>

<a href="#" onclick="history.back(-1)"> 
      Go Back
</a>
18

comments