当用户关闭浏览器选项卡时,我想根据他点击的选项发出一个 ajax 请求。
如果他点击 Leave this page -> Ajax Call 1
如果他点击 Stay on this page -> Ajax Call 2
这是我的代码现在的样子
我想在用户选择任何一个选项后执行此 ajax 调用。但是目前,如果用户尝试关闭选项卡,ajax 调用会自动运行
window.onbeforeunload = userConfirmation;
function userConfirmation(){
var cookieName = $.trim("<?php echo $this->uri->segment('5') ?>");
var toValue = $.trim($('#toValue').val());
document.cookie = cookieName+'=; expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/';
var confirmation = 'The message will be discarded.';
$.ajax({
type: 'POST',
url: "<?php echo BASE_URL.'index.php/admin/mail_actions/deleteSessionDatas' ?>",
data: {'toValue':toValue},
dataType: 'html',
success: function(response){
console.log(response);
var response = $.trim(response);
}
});
return confirmation;
}
最佳答案
好吧,我提出这是一个 hack 而不是解决方案,这只是一个解决方案。
为了执行一段代码(这不仅适用于AJAX) 只有当用户点击Stay on this page 你必须让它异步运行,因此 javaScript 保持运行同步代码(return 语句)。
jQuery.ajax() 调用必须包含 async: true 参数,然后必须包含用户点击后要执行的代码在具有适当超时的 setTimeout() 函数中 (如果页面需要太多时间来卸载超时必须更高)
var stayHere = ""; // this variable helps with the "stay on this page"_code cancellation when user chooses "leave this page"
window.onbeforeunload = userConfirmation;
function userConfirmation() {
var confirmation = "Your edits will be lost";
stayHere = setTimeout(function() { // the timeout will be cleared on "leave this page" click
$.ajax({
url: "ajax-call.php",
type: "post",
data: {chosenOpt: "stay"},
async: true, // important
success: function(data) { console.log(data);},
error: function() { alert("error");}
});
}, 2000); // Here you must put the proper time for your application
return confirmation;
}
如果用户点击离开此页面,如何运行代码
如果用户选择离开页面,页面就会开始卸载,当它完全卸载时 unload 事件会触发,所以我将代码放在它的监听器中:
jQuery(window).on("unload", function() {
clearTimeout(stayHere); // This is another assurance that the "stay here"_code is not executed
$.ajax({
url: "ajax-call.php",
type: "post",
data: { chosenOpt: "leave"},
async: false, // If set to "true" the code will not be executed
success: function(data) { console.log(data);},
error: function() { console.log("Error");} // In chrome, on unload, an alert will be blocked
});
});
注意:请注意在 unload 处理程序中执行的代码。由于 unload 事件在卸载所有内容 后触发,因此页面中的任何对象仍然可用。
这是一个jsfiddle看看它是如何工作的。
ajax-call.php 只包含
echo $_POST["chosenOpt"] . "_" . rand(1, 9999);
离开此页面时输出为leave_[number],留在此页面时为stay_[number]
注意:您可以看到 leave_[number] 仅刷新页面并且如果选中Preserve Log在控制台中。
关于Javascript - 检测用户点击导航弹出窗口的选项的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29740464/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun