javascript - Browsers back button click with confirm box -


need create javascript confirm pop on click of browsers button. if click on button pop come , "you want go ahead ?" if click on yes have redirected previous page.

i have following code not working per requirement.

if(window.history && history.pushstate){ // check history api support         window.addeventlistener('load', function(){              // create history states             history.pushstate(-1, null); // state             history.pushstate(0, null); // main state             history.pushstate(1, null); // forward state             history.go(-1); // start in main state              this.addeventlistener('popstate', function(event, state){                 // check history state , fire custom events                 if(state = event.state){                      event = document.createevent('event');                     event.initevent(state > 0 ? 'next' : 'previous', true, true);                     this.dispatchevent(event);                      var r = confirm("would save draft?");                     if(r==true) {                          // nothing                            } else {                          self.location = document.referrer;                         }                     // reset state                     history.go(-state);                  }             }, false);         }, false);     } 

any in appreciable.

window.onbeforeunload = function() {     return "leaving page reset wizard"; }; 

this you.

demo


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -