DEV Community

Cover image for How to simulate a user clicking forward and back buttons in the browser using JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to simulate a user clicking forward and back buttons in the browser using JavaScript?

Originally posted here!

To simulate a user clicking forward or back button in the navigation bar in the browser, We can use the
forward() or back() methods available in the History API.

Simulating a forward click

To simulate a forward click we can use the forward() method like this,

// Simulate forward click
window.history.forward();
Enter fullscreen mode Exit fullscreen mode

Simulating a back click

To simualte a back click we can use the back() method like this,

// Simulate back click
window.history.back();
Enter fullscreen mode Exit fullscreen mode

Feel free to share if you found this useful 😃.


Top comments (0)