cancel
Showing results for 
Search instead for 
Did you mean: 

Browser back Button handle

Former Member

Hi All,

how can i handle the Browser Back Button in SAPUI 5? I want that when the user clicks on the button "back" of the browser, it is possible to display a popup that alerts that there is some data not saved.

Thank you,

Bilel

Edit (Solution):

document.onmouseover = function() {
     //User's mouse is inside the page.
     window.innerDocClick = true;
}
document.onmouseleave = function() {
    //User's mouse has left the page.
    window.innerDocClick = false;
}
window.onhashchange = function() {
   if (window.innerDocClick != false) {
      window.innerDocClick = false;
   } else {
     //Browser back button was clicked
     me.checkDataModified(true);
  }
}
former_member251534
Participant

Where did you write this code... Could you please share... I am facing same issue

Accepted Solutions (1)

Accepted Solutions (1)

irfan_gokak
Contributor
0 Kudos

Hi,

Try this function.

window.onhashchange = function() {
    if (window.innerDocClick) {
        //Your own in-page mechanism triggered the hash change
    } else {
        //Browser back button was clicked
    }
}
Former Member
0 Kudos

Hi,
it works on all the buttons even the menu of the application, i want a function that handle only the browser back button.

thanks,
Bilel

Former Member

I have modified your code and it's worked:

document.onmouseover = function() {
     //User's mouse is inside the page.
     window.innerDocClick = true;
}
document.onmouseleave = function() {
    //User's mouse has left the page.
    window.innerDocClick = false;
}
window.onhashchange = function() {
   if (window.innerDocClick != false) {
      window.innerDocClick = false;
   } else {
     //Browser back button was clicked
     me.checkDataModified(true);
  }
}

Answers (2)

Answers (2)

pmguti
Explorer

Hi,

I have added those functions on the init function and now even if I am on another page, the functions are triggered. How can I narrow the impact of those functions ONLY for the page and controller I am setting it up?

Thanks in advance for your help

soumya_r
Participant

Hi,

The above solution works only for single page/view applications.

For the 2 or more views application, you can call these functions in the objectpatternmatched method which gets triggered when the pattern matched or onbeforerendering method of the controller. So, it will always triggered on pattern matched.

I have faced the same issue and it got solved.

Thanks,

Soumya

OwenLiu
Product and Topic Expert
Product and Topic Expert
0 Kudos

Thanks for coming to SAP Community for answers. Please post your question as a new question here:

https://answers.sap.com/questions/ask.htmlSince you're new in asking questions here, check out our tutorial about asking and answering questions (if you haven't already), as it provides tips for preparing questions more effectively, that draw responses from our members. Please note, that your post here won't be answered.

irfan_gokak
Contributor
0 Kudos

Hi,

Below code tested in Chrome browser. Please check.

window.onbeforeunload = function() { return "You work will be lost."; };
Former Member
0 Kudos

it works only when the refresh button was clicked.