我正在编写一个表单,我需要像stackoverflow这样的函数:"你已经开始编写或编辑帖子了."
我已经查看了stackoverflow的代码,看看他们是如何做到这一点的,但我根本没有把它应用到我的简单表单中.这就是他们对question.js的看法:
function initNavPrevention(b) {
if (b.which == "undefined") {
return
}
var a = $("#wmd-input");
a.unbind("keypress", initNavPrevention);
setConfirmUnload("You have started writing or editing a post.", a)
}
Run Code Online (Sandbox Code Playgroud)
其中一个函数触发了这个(我不知道c是什么):
if (c) {
e.keypress(initNavPrevention)
}
Run Code Online (Sandbox Code Playgroud)
最后他们必须setConfirmUnload(null);禁用它我想.
我的情况很简单.我有一个页面,我加载了一个<form />通过AJAX,我想阻止加载此表单离开页面而不点击SUBMIT按钮或如果用户单击取消,表单被禁用,预防将不会弹出.
拥有此代码,有人可以告诉我如何将其包含在我的网站上吗?
这是我加载表单的AJAX函数:
$("#projects_historics_edit_part_1").click(function(){
$("#ajax-loader").fadeIn('normal');
$.ajax({
url: BASE_URL + 'projects/ajax/get_historics_edit_part_1/' + PROJECT_ID,
type: "POST",
success: function(data) {
$("div#projects_historics_part_1").html(data);
$("#client").autocomplete(client_autcomplete_options);
var inicofin = $("#initdate, #findate").datepicker(initdate_datepicker_options);
}
});
$("#ajax-loader").fadeOut("normal");
return false;
});
Run Code Online (Sandbox Code Playgroud)
先感谢您!
我目前正在检测我的网站用户是否关闭了窗口/标签或更改了网址.
我正在使用以下代码,它完美地运行:
var validNavigation = false;
function endSession() {
// Browser or broswer tab is closed
// Do sth here ...
alert("bye");
}
function wireUpEvents() {
window.onbeforeunload = function() {
if (!validNavigation) {
endSession();
}
}
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event …Run Code Online (Sandbox Code Playgroud) 我知道有很多关于这方面的问题,但没有什么能正确回答我.我想在用户离开页面时显示确认对话框.如果用户按下取消,他将保持在页面上,如果确定,他所做的更改将通过调用方法回滚.我这样做了:
window.onbeforeunload = function () {
var r = confirm( "Do you want to leave?" );
if (r == true) {
//I will call my method
}
else {
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
问题是我正在获取浏览器默认弹出窗口:"LeavePage/StayOnPage"
此页面要求您确认是否要离开 - 您输入的数据可能无法保存.
此消息显示在Firefox中,在Chrome中有点不同.我在第一个确认对话框上按OK后弹出这个弹出窗口.
有没有办法不显示这个对话框?(第二个,我没有创建).或者,如果有任何方法可以控制此弹出窗口,有没有人知道如何做到这一点?谢谢
我正在Google Chrome中测试.
我做了一些搜索,发现有人正在使用:
window.onbeforeunload = function() {
if (hook) {
return "Did you save your stuff?"
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用它时,我仍然得到"你所做的改变可能无法保存." 信息.如何将其更改为我想要的内容?
如何询问用户您确定要离开页面吗?
例如,如果在Stackoverflow上提问时单击后退按钮?
我在我的网页上有一个javascript编辑器,我想询问用户他/她是否想要离开页面,即使有未保存的更改.
我知道我可以通过这种方式向"onbeforeunload对话框"添加自定义消息:
window.onbeforeunload = function() {
return 'You have unsaved changes!';
}
Run Code Online (Sandbox Code Playgroud)
(来源)但我想只显示一些未保存的更改的对话框.怎么做?
谢谢!
我想在关闭窗口之前向用户显示一条消息.我正在使用运行良好的SweetAlert(http://tristanedwards.me/sweetalert).
问题在于JavaScript/jQuery让我知道用户何时尝试关闭窗口/标签然后显示阻止他关闭页面的内容,除非他再次点击.
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
swal("Here's a message!");
return "You have attempted to leave this page. Are you sure?";
}
</script>
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它在我的SweetAlert上显示了丑陋的常见信息,任何想法?没有返回部分它无论如何关闭窗口,我试过:/
当有人尝试刷新浏览器时,我试图创建一个自定义弹出卡。我知道如何使用它在浏览器顶部显示弹出警报。
window.onbeforeunload = function() {
return 'Your upload will be lost if you leave the page, are you sure?';
};
Run Code Online (Sandbox Code Playgroud)
我有一个Vue.JS应用程序,该beforeDestroy方法试图在关闭或重新加载应用程序之前将一些数据存储在本地存储中。仅供参考:
beforeDestroy: function() {
localStorage.setItem('preference', this.preference);
...
}
Run Code Online (Sandbox Code Playgroud)
但是,关闭或重新加载应用程序实际上都不会调用此方法。如何确保在关闭应用程序之前调用此方法?
javascript ×9
jquery ×5
reactjs ×2
popup ×1
react-router ×1
refresh ×1
sweetalert ×1
vue.js ×1