Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  111] [ 1]  / answers: 1 / hits: 39799  / 9 Years ago, thu, july 9, 2015, 12:00:00

I need to disable the back button of browser in my single page application.
I tried using methods like onhashchange or window.history.forward but they don't work (reason may be that url doesn't get changed here)


More From » jquery

 Answers
135

I work in AngularJS building a Single Page App and I wanted to disable or prevent the back button of the browser, because my app has buttons and anchors for all navegation into my app.



I searched and test many codes, and this is the easy to prevent the back button of browsers and the following code worked for me.




window.onpopstate = function (e) {
window.history.forward(1); }




When the history detects the first history.back()




window.onpopstate




is executed, then after this moment any back or forward in history are detected for onpopstate event.


[#65869] Tuesday, July 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
braydon

Total Points: 0
Total Questions: 102
Total Answers: 111

Location: Sao Tome and Principe
Member since Wed, Dec 29, 2021
2 Years ago
;