简体   繁体   中英

How to Enable/Disable browser history through js

can we enable or disable browser history through java script code ?? If possible please tell me the mechanism to do this .

I think it is not possible.

Can you try using document.location.replace() it is used to clear the last entry in the history and replace it with the address of a new url.

If the url or url-fragment changes, entries will be added to the browser history. Means, the browser will display the navigation buttons. When a user clicks them the previous url, or fragment is shown in the address bar. Your application can ignore it do something with it. If are looking for a single page app kind of behavior with no history and navigation links what so ever, then you should not change the initial url or part of it.

This post can be helpful: How to clear browsers (IE, Firefox, Opera, Chrome) history using JavaScript or Java except from browser itself?

JS Frameworks like YUI suggest that we cannot remove the history items but you can do some other stuff with History. Excerpts from YUI docs -

While it's not possible to remove an entry from the browser history, it is possible to create a new entry (or replace the current entry) and remove one or more state values that were previously set. To do this, add or replace one or more values with null or undefined.

Refer this for more information http://yuilibrary.com/yui/docs/history/

if you need enable for history supported browsers there is Modernizr plugin you can use, Unsupported history browsers it will be disabled.

 if (Modernizr.history) {
        //Code goes here that works if it's HTML 5 Browser with .history support? 
       //  I know some HTML5     browsers deal with .history oddly (read buggy) so what
       //   happens in      those cases?
     } else {
      // disable code
   }

check this link https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM