我有一个小问题.我试图抓住窗口的OnUnLoad事件并询问确认问题,如果用户决定他们想要保持那么好,如果他们想离开页面,那么他们将丢失所有未保存的数据.这是问题......
我正在使用jQuery UI对话框,当我在页面上放置以下代码时,我打开了Dialog,当我单击浏览器上的后退按钮时,它永远不会弹出msgbox.它只刷新页面:
<script type="text/javascript">
$(window).bind('beforeunload', function() {
alert('you are an idiot!');
}
);
</script>
Run Code Online (Sandbox Code Playgroud)
我正在使用的解决方案是这里的帖子.再次,如果我没有打开jQuery UI对话框,msgbox将显示正常.如果我这样做,那么它不会显示msgbox并只刷新页面.
有任何想法吗?
我正在尝试在卸载页面之前对服务器发帖,然后我按照这个并且它工作正常.我的问题是关于window.unload的$.员额被触发后它已卸载.我尝试使用注销链接并检查我的日志,我得到以下内容:
Started GET "/signout" for 127.0.0.1 at 2012-11-22 00:15:08 +0800
Processing by SessionsController#destroy as HTML
Redirected to http://localhost:3000/
Completed 302 Found in 1ms
Started GET "/" for 127.0.0.1 at 2012-11-22 00:15:08 +0800
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (0.4ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 13ms (Views: 12.9ms)
Started POST "/unloading" for 127.0.0.1 at 2012-11-22 00:15:08 +0800
Processing by HomeController#unloading as */*
Parameters: {"p1"=>"1"}
WARNING: Can't verify CSRF token authenticity
Completed 500 …Run Code Online (Sandbox Code Playgroud) 我有一个包含文本框和提交按钮的网页.当用户编辑文本框中的文本并单击另一个链接(而不是提交按钮)时,如何显示"您确定要离开此页面"弹出消息吗?
我在网上研究了这个,发现了一些javascript示例.这是你能做到这一点的唯一方法吗?如果没有,最好的方法是什么?
我想在用户尝试重新加载页面时显示一个确认框.我在我的项目中尝试过它并没有用.我已经创建了一个我尝试过的示例代码,即使它不起作用.任何人都可以告诉我哪里出错了.我甚至没有收到确认框.我使用的是Chrome 33+.我需要在弹出框之前在window.onbeforeunload中执行一些代码.
<!DOCTYPE>
<html>
<body>
<script type="text/javascript">
window.onbeforeunload = function()
{
var r = confirm("Are you sure you want to reload the page.");
if(r)
{
window.location.reload();
}
else
{
}
};
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我想知道当用户试图离开页面时如何显示确认框,例如当您尝试关闭"提问"页面时,此处在StackOverflow中.
提前谢谢你.
我有一个简单的html表单,其中包含很少的字段.
我想确保用户在尝试离开页面时收到警告消息.所以我在页面上添加了脚本,只要用户试图离开此页面,该脚本就会运行.
但是当用户尝试提交表单时,我也收到此警报消息.当用户保存表单时,我不想要此警报消息.
请帮忙.
下面是一些示例代码.
<html>
<head>
<script>
function verifyexit() { return 'Are you sure ?'; } window.onbeforeunload = verifyexit;
</script>
</head>
<body>
Go to <a href="b.html"> another page </a>
<form action="c.html">
name : <input type="text" name="name" value=""/> <br>
email : <input type="text" name="email" value=""/> <br>
<input type="submit" value="save"/>
</form>
</body>
Run Code Online (Sandbox Code Playgroud)
我允许用户在表单字段中进行一些更改,并根据用户是否进行了一些更改,启用"提交更改"按钮.
我想添加一个功能,我要求用户确认他想要导航到其他页面而不提交他所做的更改.我添加了代码来显示一个对话框,但问题是对话框显示的时间很短,然后页面导航移动到另一个页面.我的代码是:
window.onbeforeunload = function() {
if(!dijit.byId('editBtn').get('disabled')) {
discardChanges(
'Pending Unsaved Changes',
'You have pending unsaved changes. Do you really want to discard them?',
'Discard Changes',
function() {
return true;
},
function() {
return false;
},
300,
700);
}
}
Run Code Online (Sandbox Code Playgroud)
如何阻止浏览器移动到其他页面并等待用户单击对话框上的按钮,然后确定是否要丢弃单击.对话框如下所示:

所以,这里我只想在窗口关闭时提醒true(我的意思是浏览器中的特殊标签).
$(document).ready(function(){
});
$(window).unload(function(){
alert('true'); });
Run Code Online (Sandbox Code Playgroud)
$(window)里面也尝试过$(document).ready(),没什么.
我想在用户单击浏览器“后退按钮”时警告用户,然后在确认后重定向。下面是我的 JS 代码,但仅适用于 Firefox,我想让它也适用于 chrome 和其他浏览器。
注意:为了在 chrome 中触发事件,我需要先点击页面正文,然后点击浏览器的“后退”按钮(这不好)。
请协助。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page2</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script>
$(function(){
window.history.pushState({page: 1}, "", "");
window.onpopstate = function(event) {
if(event){
var confirm = window.confirm("Please, note that you may lose your move details by returning to the previous page.");
}
}
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)