Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

javascript - onBeforeUnload not working correctly for Chrome

I've been using this code for some time and it worked fine until yesterday:

window.onbeforeunload = function() {
    return "?Atención! Si continúas no podrás volver atrás y podrías perder datos. Revisa todos los campos y termina el trabajo antes de moverte de esta página. ?Seguro que quieres continuar?";
}

The problem now is that Chrome shows this:

Chrome screenshot

But Edge shows it the right way:

Edge screenshot

What am I doing wrong?

Note: Language is not the problem because I've tried with both English and Spanish.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're not doing anything wrong. Google has decided to do what Mozilla did and not allow you to show a custom message anymore. It took effect in Chrome 51.

You still need to return something to trigger the built-in dialog, but you're no longer allowed to determine what that dialog says.

Here's the BugZilla entry where Mozilla decided to do it in Firefox.

Here's the Chrome issue where Google decided to do it in Chrome.

*(The new Chrome 51 behavior was reported as a bug, but naturally it was closed within hours as it's by design.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...