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

in #javascript7 years ago (edited)

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>
Sort:  

Congratulations @thedevchick! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You got a First Reply

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @thedevchick! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!