2

i have a typical web application with multi language ( french , english and Nederland ) and I'm using a page unload trigger to warn the user of unsaved changes while leaving the page/closing the tab/etc... and this works fine.

My problem is how can i change language of onbeforeunload message with title of dialogbox and the 2 bottons "leave this page" and "stay on this page" without changing language of the browser?

4 Answers4

2

The browser's default UI for the onbeforeunload dialog currently cannot be changed because it'd be very easy to abuse it:

  • Customizing the browser's dialog would allow developers to fool users into staying in the page (e.g. changing the buttons' text content or hiding them).
  • You cannot replace the onbeforeunload dialog with your own code as then the developer would decide when it is ok to close a window, and that is not how browsers work.

The user is in control of the browser, not the developer. Hence these restrictions.

Just assume that the user's browser is in a language which s/he can understand, and s/he can figure out from there. That's the best we have at the moment.

See related thread

This may change in the future with Shadow DOM and new APIs, but I cannot think of any way that wouldn't be easily abused (except by requesting a "prevent closing" API similarly to how browsers handle getUserMedia). Feel free to suggest it to WHATWG if you feel like this is an important feature which will help other developers.

Community
  • 1
  • 1
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
0

When you spit out the page with PhP, and you get to the point of the message that is needed for the javascript, echo the message in the right language.

Jelle Ferwerda
  • 1,254
  • 1
  • 7
  • 13
0

Do it by HTML/javascript. In HTML code of page add var like this:

<script> 
    onBeforeUnloadMessage = "<% $MESSAGE_FROM_SERVER %>";
</script>

Use this variable in your javascript:

window.onbeforeunload = function () {
    return onBeforeUnloadMessage;
}
Alex
  • 11,115
  • 12
  • 51
  • 64
0

you can use Language translator plugin of js

http://www.jquery4u.com/plugins/10-jquery-translator-plugins/

Rohan Patil
  • 1,865
  • 1
  • 23
  • 36