简体   繁体   English

如何在JavaScript中显示对话框?

[英]How to show a dialog in javascript?

I need to through alert message like "Are you want to navigate?" 我需要通过“您是否要导航?”之类的警报消息 when user will modify some data but did not save that and trying to navigate to the other tab. 当用户将修改一些数据但没有保存并尝试导航到其他选项卡时。

I want yes or No functionality on clicking on other tab. 我要单击其他选项卡上的是或否功能。 Not leave this page or stay on this. 不要离开此页面或留在此。

Kinldy help me. 请帮我。 Thanks In advanced. 提前致谢。

function goodbye(e) {
  if (!e) e = window.event;
  //e.cancelBubble is supported by IE - this will kill the bubbling process.
  e.cancelBubble = true;
  e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog

  //e.stopPropagation works in Firefox.
  if (e.stopPropagation) {
    e.stopPropagation();
    e.preventDefault();
  }
}
window.onbeforeunload = goodbye;

I have used this but it is not showing yes/no.and in same page some buttons are there. 我已经使用了它,但是它没有显示是/否。在同一页面中,有一些按钮。 After clicking this page is refreshing and this message is coming. 单击后,此页面正在刷新,并且此消息即将到来。 can some one plz help me. 有人可以帮我吗。

You may use beforeunload event. 您可以使用beforeunload事件。
Your handler should return a string, which will be displayed to a user. 您的处理程序应返回一个字符串,该字符串将显示给用户。

The simplest usage example: 最简单的用法示例:

window.onbeforeunload = function() {
    return "Do you want to navigate away?";
};

If you have any other questions, refer to the documentation . 如有其他疑问,请参阅文档

If you just want to display some info, then window.alert(Info) could be useful. 如果只想显示一些信息,则window.alert(Info)可能会有用。 Since you need to take opinion from user, you can use window.confirm(your Question) 由于您需要征询用户的意见,因此可以使用window.confirm(您的问题)

confirm("Do you want to leave this page?");

Confirm is similar to an alert box that popups up.But it has a difference,it gives the user two options: 'Ok' and 'cancel'. 确认类似于弹出的警报框。但有所不同,它为用户提供两个选项:“确定”和“取消”。 If the user clicks 'ok' button,the window will unload and the user will navigate from the page successfully. 如果用户单击“确定”按钮,则将卸载该窗口,并且用户将成功从页面导航。 Confirm is often used for verfication and confirmation as the name suggests. 顾名思义,Confirm通常用于验证和确认。

  <html> <body> <p>Click the button to demonstrate line-breaks in an alert box.</p> <button onclick="myFunction()">click it</button> <script> function myFunction() { alert("Do you want to navigate away ?"); } </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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