在stackoverflow中,如果您开始进行更改,那么您尝试离开页面,会出现一个javascript确认按钮并询问:"您确定要离开此页面吗?" blee blah bloo ...
有没有人以前实现过这个,我如何跟踪提交的更改?我相信我自己可以做到这一点,我正在努力学习专家们的良好做法.
我尝试了以下但仍然无法正常工作:
<html>
<body>
<p>Close the page to trigger the onunload event.</p>
<script type="text/javascript">
var changes = false;
window.onbeforeunload = function() {
if (changes)
{
var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
if (confirm(message)) return true;
else return false;
}
}
</script>
<input type='text' onchange='changes=true;'> </input>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
任何人都可以发一个例子吗
有谁知道如何阻止页面重新加载或导航?
jQuery(function($) {
/* global on unload notification */
warning = true;
if(warning) {
$(window).bind("unload", function() {
if (confirm("Do you want to leave this page") == true) {
//they pressed OK
alert('ok');
} else {
// they pressed Cancel
alert('cancel');
return false;
}
});
}
});
我目前正在开发电子商务网站,显示您未来订单的页面可以使用+/-按钮更改订购商品的数量.以这种方式改变数量实际上不会改变订单本身,他们必须按下确认,因此提交一个积极的行动来改变订单.
但是,如果他们更改了数量并离开了页面,我想警告他们他们这样做是为了防止这是一次意外,因为如果他们离开或刷新页面,更改的数量将会丢失.
在上面的代码中,我使用的是一个全局变量,默认情况下为false(仅适用于测试),当数量更改时,我将更新此变量为true,当他们确认更改时,我将其设置为false .
如果警告为真并且页面已卸载,我会向他们提供一个确认框,如果他们拒绝他们想留在这个页面我需要阻止它卸载.return false不起作用,它仍然允许用户导航(警报仅用于调试)
有任何想法吗?
我有一个小问题.我试图抓住窗口的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并只刷新页面.
有任何想法吗?
我有一个问题,当我试图检查一个有趣页面的来源,每隔3-5秒自动刷新一次(可能是由于一些js脚本),每次刷新页面时都会重置我的Inspect Element Inspector窗口.
除了打开NoScript以阻止页面自动刷新之外,还有其他方法可以阻止该页面刷新或者Inspector窗口重置自身吗?
我的网络应用程序有这个:
$(window).bind('beforeunload', function() {
if(unSavedChanges == true)
{
return 'You have unsaved changes';
}
return null;
});
Run Code Online (Sandbox Code Playgroud)
这在Safari和Firefox中运行良好(除了firefox没有在其onBeforeUnload对话框中显示我的自定义消息).但是在Windows 7上的IE 8上,它总是显示onBeforeUnload通知,特别是如果没有未保存的更改,它只会说"null".当用户保存所有内容并希望导航时,如何阻止IE显示onBeforeUnload通知?
根据Jon的建议,我删除了该return null行,代码现在读取
$(window).bind('beforeunload', function() {
if(unSavedChanges == true)
{
return 'You have unsaved changes';
}
});
Run Code Online (Sandbox Code Playgroud) 当用户试图离开时,我试图给出一个警报确认对话框...我有以下代码,但它不太有效......
jQuery(window).unload(function() {
var yes = confirm("You're about to end your session, are you sure?");
if (yes) {
return true;
} else {
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
弹出窗口很好但是当我点击"否"时,它仍然会导航......
谢谢你的期待......
如何说服Angular 4路由器在我的应用程序中禁用直接浏览器导航?
我已经查看了路由器钩子CanActivate并CanDeactivate实现了它们,但CanDeactivate在使用直接浏览器导航时根本不会触发.
我可以CanActivate用来阻止网址,但只有在应用程序重新启动后,这需要时间和浏览器窗口中的不需要的更改.
我希望能够在应用重新启动之前捕获直接浏览器导航.
如果这有帮助:我想完全禁止直接浏览器导航,所以我不需要允许某些网址并禁用其他网址的解决方案.
这是我的路由器定义:
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent, canActivate: [RouteGuardService] },
{ path: 'users', component: UsersMainComponent, canActivate: [RouteGuardService] },
{ path: 'vendors', component: VendorMainComponent, canActivate: [RouteGuardService] },
{ path: 'invoices', component: InvoicesMainComponent, canActivate: [RouteGuardService] },
{ path: 'offers' , component: EsdMainComponent, canActivate: [RouteGuardService] },
{ path: 'settings' , component: SettingsMainComponent, canActivate: [RouteGuardService] },
// Callback …Run Code Online (Sandbox Code Playgroud) 我希望阻止浏览器在任何情况下关闭页面或在其他情况下阻止浏览器在onbeforeunload 调用时执行任何操作.这是我试过的代码.
(function() {
var proxied = window.onbeforeunload;
window.onbeforeunload = function(e) {
e.preventDefault();
e.stopPropagation();
//i want to stop everything
console.log('stay here');
// return 'message';
};
})();
Run Code Online (Sandbox Code Playgroud)
我想知道当用户试图离开页面时如何显示确认框,例如当您尝试关闭"提问"页面时,此处在StackOverflow中.
提前谢谢你.
confirmExit()当用户试图离开页面时,我使用下面的代码来激活一个函数:
window.onbeforeunload = confirmExit;
function confirmExit(){
return "Are you sure you want to exit this page?";
}
Run Code Online (Sandbox Code Playgroud)
此外:
该页面还会触发AJAX函数以"即时"更新数据库.因此,当用户离开页面时,我需要删除所有"即时"数据.
当用户点击"离开页面"按钮时,是否可以调用javascript函数?
我正在尝试激活页面重新加载器预防器,并能够停用重新加载器预防器。换句话说,当用户即将卸载/重新加载其页面时,我能够显示一个对话框,但我目前正在寻找一种方法,让用户在某个功能之后卸载/重新加载其页面,而无需任何对话框或警告已被调用。
也许删除事件侦听器会有帮助?
这是我正在使用的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="activateReloader()">activate</button>
<button onclick="deactivateReloader()">deactivate</button>
<script>
function activateReloader() {
window.addEventListener('beforeunload', function (e) {
// Cancel the event
e.preventDefault();
// Chrome requires returnValue to be set
e.returnValue = '';
});
}
function deactivateReloader() {
window.removeEventListener('beforeunload', function (e) {});
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)