0

I want to disable browser back and forward,so that i got code like this

(function (global) {

    if(typeof (global) === "undefined") {
        throw new Error("window is undefined");
    }

    var _hash = "!";
    var noBackPlease = function () {
        global.location.href += "#";

        // making sure we have the fruit available for juice (^__^)
        global.setTimeout(function () {
            global.location.href += "!";
        }, 50);
    };

    global.onhashchange = function ($valuable) {
        if (global.location.hash !== _hash) {
            global.location.hash = _hash;
        }
    };

    global.onload = function () {            
        noBackPlease();

        // disables backspace on page except on input fields and textarea..
        document.body.onkeydown = function (e) {
            var elm = e.target.nodeName.toLowerCase();
            if (e.which === 8 && (elm !== 'input' && elm  !== 'textarea')) {
                e.preventDefault();
            }
            // stopping event bubbling up the DOM tree..
            e.stopPropagation();
        };          
    }

})(window);

It is working fine, but in url redirection page name along with #! is coming .How to remove this

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • Is there a reason you add them in the first place? if not, just remove `noBackPlease()` – phuzi Nov 30 '17 at 09:15
  • iam not geting your question @phuzi –  Nov 30 '17 at 09:16
  • They are add to the url in the `noBackPlease()` function – phuzi Nov 30 '17 at 09:16
  • 1
    Possible duplicate of [how to stop browser back button using javascript](https://stackoverflow.com/questions/12381563/how-to-stop-browser-back-button-using-javascript) – GGO Nov 30 '17 at 09:17
  • i want noBackPlease() but i dont want #! in url –  Nov 30 '17 at 09:17
  • @Nadh Can't have both, sorry – phuzi Nov 30 '17 at 09:18
  • You cannot disable history nagivation since it is a browser behaviour. Spamming browser history with fake items is not a good idea as well. Do not create malicious sites - create websites where user would like to stay himself. If you want to prevent losing data, then just use `onbeforeunload`. – Yeldar Kurmangaliyev Nov 30 '17 at 09:18
  • iam using this, its working fine is this correct way? –  Nov 30 '17 at 09:47

0 Answers0