自定义确认框,具有一些带有窗口关闭事件的功能

发布于 2025-01-08 06:20:47 字数 187 浏览 2 评论 0原文

我目前正在编写 JavaScript,单击窗口中的关闭按钮时,我应该会看到一个确认框。在确认框中,我应该显示一些消息,并且应该有取消和继续按钮。

单击“取消”时,不应关闭窗口,但按“继续”时,窗口应重定向到另一个 jsp。

有人可以帮我解决这个代码吗?我尝试使用自定义确认框,但这似乎只返回一个字符串,并且不能用于重定向到页面。

I'm presently writing JavaScript, on clicking the close button in the window, I should get a confirm box. In the confirm box, I should display some message and there should be cancel and continue buttons.

On clicking cancel, the window should not be closed, but on pressing continue, the window should be redirected to another jsp.

Can someone please help me with this code? I tried using the custom confirm box, but that seems to return only a string and it cannot be used to redirect to a page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2025-01-15 06:20:47

这是不可能的。当用户关闭窗口时,您无法将用户重定向到另一个页面。这是出于安全原因。

要在关闭窗口时显示确认框,可以使用 onbeforeunload 事件。这将询问用户是否希望离开该页面。确认框由浏览器呈现,您可以自定义的只是文本。

$(window).bind('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

当用户离开页面时,您可以使用 onunload 事件,但同样,您无法重定向它们(您可以进行 AJAX 调用,但无法重定向浏览器)。

$(window).bind('unload', function(){
  console.log('bye'); // Some browsers may block this.
                      // Chrome blocks alerts in this event.
});

This is impossible. You cannot redirect the user to another page when they close the window. This is for security reasons.

To display a confirm box when closing the window, you can use the onbeforeunload event. This will ask the user if they wish to leave the page or not. The confirm box is rendered by the browser, all you can customize on is the text.

$(window).bind('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

When the user leaves the page, you can use the onunload event, but again, you cannot redirect them (you can make an AJAX call, but you cannot redirect the browser).

$(window).bind('unload', function(){
  console.log('bye'); // Some browsers may block this.
                      // Chrome blocks alerts in this event.
});
山人契 2025-01-15 06:20:47

检查这个示例,它可能会帮助您使用

function setConfirmUnload(on){
   window.onbeforeunload = (on) ? unloadMessage : null;
}

function unloadMessage(){
   return "Are you sure you want to leave this page";
}

setConfirmUnload(true) 来启用确认,如果您想让他们在没有警告的情况下关闭屏幕(例如,如果您有关闭按钮),则为 false。

您也可以尝试这个(链接:http://api.jquery.com/unload/

$(window).unload(function(){
   alert("Bye now!");
}); 

check this example it may help you out

function setConfirmUnload(on){
   window.onbeforeunload = (on) ? unloadMessage : null;
}

function unloadMessage(){
   return "Are you sure you want to leave this page";
}

setConfirmUnload(true) to enable the confirmation or false if you want to allow them to close the screen without a warning (if you have a close button for instance).

You can try also this (link : http://api.jquery.com/unload/)

$(window).unload(function(){
   alert("Bye now!");
}); 
つ可否回来 2025-01-15 06:20:47

您需要将处理程序绑定到 onBEFOREunload 事件,因为 onunload 事件无法实现您想要的任何功能(为时已晚)。

var myFunction = function () {
    return "Are you sure you want to leave the page?";
};

window.onbeforeunload = myFunction;

据我所知,由于确认框是由浏览器处理的,因此无法对用户输入做出反应。

You need to bind a handler to the onBEFOREunload event, as none of the functionality you want will be possible with the onunload event (it is too late).

var myFunction = function () {
    return "Are you sure you want to leave the page?";
};

window.onbeforeunload = myFunction;

As far as I know, there is no way to react to the user input as the confirm box is handled by the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文