Sometimes, we want to make an HTML back link with JavaScript.
In this article, we’ll look at how to make an HTML back link with JavaScript.
How to make an HTML back link with JavaScript?
To make an HTML back link with JavaScript, we call history.back
when we click on the link.
For instance, we write
<a id="back-link">back</a>
to add a link.
Then we write
const element = document.getElementById("back-link");
element.onclick = (e) => {
e.preventDefault();
history.back();
};
to select the link with getElementById
.
And then we set its onclick
property to a function that calls preventDefault
to stop the default link behavior.
Then we call history.back
to go back to the previous page.
Conclusion
To make an HTML back link with JavaScript, we call history.back
when we click on the link.