自定义确认框,具有一些带有窗口关闭事件的功能
我目前正在编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是不可能的。当用户关闭窗口时,您无法将用户重定向到另一个页面。这是出于安全原因。
要在关闭窗口时显示确认框,可以使用
onbeforeunload
事件。这将询问用户是否希望离开该页面。确认框由浏览器呈现,您可以自定义的只是文本。当用户离开页面时,您可以使用
onunload
事件,但同样,您无法重定向它们(您可以进行 AJAX 调用,但无法重定向浏览器)。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.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).检查这个示例,它可能会帮助您使用
setConfirmUnload(true)
来启用确认,如果您想让他们在没有警告的情况下关闭屏幕(例如,如果您有关闭按钮),则为 false。您也可以尝试这个(链接:http://api.jquery.com/unload/)
check this example it may help you out
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/)
您需要将处理程序绑定到 onBEFOREunload 事件,因为 onunload 事件无法实现您想要的任何功能(为时已晚)。
据我所知,由于确认框是由浏览器处理的,因此无法对用户输入做出反应。
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).
As far as I know, there is no way to react to the user input as the confirm box is handled by the browser.