Skip to main content

Command Palette

Search for a command to run...

Do you know history.back() in JavaScript?

Published
β€’1 min read
Do you know history.back() in JavaScript?
D

I am 17 years old and a young passionate and self-taught frontend web developer and have an intention to become a successful developer. I usually write about JavaScript and Web Development and share some tips in the articles.

So, we all surf the internet to watch YouTube videos and Stackoverflow and many more. And we usually click "←" this button to go back to the previous website.

back button

Do you know? You can create the same functionality of "←" this button of browser in Javascript πŸ”₯


Let's create a back button

<button>Go Back</button>

It's time to make it functional ✌️

  • Add onclick attribute to the button
    onclick="history.back()"
    
  • Here's the final and little code πŸ™‚
    <button onclick="history.back()">Go Back</button>
    

How does this work?

The history.back() method loads the previous URL from the history list. It will only work if the previous page exists.

Try it live by yourself: Live demo (Open this link in this tab only since it will create a previous page πŸ™‚ )


Feel free to reach me out via Twitter - @codewithsnowbit

🌏 Let's connect

Stay tuned for the next article. Stay safe and Happy Coding!

More from this blog

D

Dhairya Shah

119 posts