Javascript';onbeforeunload()和#x27;不使用函数调用
我下面有一个脚本,用于调查。我遇到的问题是,onbeforeunload()在我不调用其中的函数时起作用。如果我在其中进行任何函数调用(save_survey()或fetch_demographics()),浏览器或选项卡将关闭,而不会出现任何提示Javascript';onbeforeunload()和#x27;不使用函数调用,javascript,onbeforeunload,Javascript,Onbeforeunload,我下面有一个脚本,用于调查。我遇到的问题是,onbeforeunload()在我不调用其中的函数时起作用。如果我在其中进行任何函数调用(save_survey()或fetch_demographics()),浏览器或选项卡将关闭,而不会出现任何提示 <script type="text/javascript"> $(document).ready(function() { $('#select_message').hide(); startTime = n
<script type="text/javascript">
$(document).ready(function() {
$('#select_message').hide();
startTime = new Date().getTime();
});
loc = 0;
block_size = {{ block_size }};
sid = {{ sid }};
survey = {{ survey|tojson }};
survey_choices = '';
startTime = 0;
demographics_content = {};
function save_survey(sf)
{
var timeSpentMilliseconds = new Date().getTime() - startTime;
var t = timeSpentMilliseconds / 1000 / 60;
var surveydat = '';
if(sf==1)
{ //Success
surveydat = 'sid='+sid+'&dem='+JSON.stringify(demographics_content)+'&loc='+loc+'&t='+t+'&survey_choice='+JSON.stringify(survey_choices);
}
if(sf==0)
{ //Fail
surveydat = 'sid='+sid+'&dem='+json_encode(demographics_content)+'&loc='+loc+'&t='+t+'&survey_choice='+json_encode(survey_choices);
}
//Survey Save Call
$.ajax({
type: 'POST',
url: '/save_surveyresponse/'+sf,
data: surveydat,
beforeSend:function(){
// this is where we append a loading image
$('#survey_holder').html('<div class="loading"><img src="/static/img/loading.gif" alt="Loading..." /></div>');
},
success:function(data){
// successful request; do something with the data
$('#ajax-panel').empty();
$('#survey_holder').html('Success');
alert("Dev Alert: All surveys are over! Saving data now...");
window.location.replace('http://localhost:5000/surveys/thankyou');
},
error:function(){
// failed request; give feedback to user
$('#survey_holder').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
}
});
}
function verify_captcha()
{
// alert($('#g-recaptcha-response').html());
}
function block_by_block()
{
var div_content ='<table border="0" cellspacing="10" class="table-condensed"><tr>';
var ii=0;
var block = survey[loc];
var temp_array = block.split("::");
if(loc>=1)
{
var radio_val = $('input[name=block_child'+(loc-1)+']:checked', '#listform').val();
//console.log(radio_val);
if(radio_val!=undefined)
survey_choices += radio_val +'\t';
else
{
alert("Please select one of the choices");
loc--;
return false;
}
}
for(ii=0;ii<block_size;ii++)
{
//Chop the strings and change the div content
div_content+="<td>" + temp_array[ii]+"</td>";
div_content+="<td>" + ' <label class="btn btn-default"><input type="radio" id = "block_child'+loc+'" name="block_child'+loc+'" value="'+temp_array[ii]+'"></label></td>';
div_content+="</tr><tr>";
}
div_content+='<tr><td><input type="button" class="btn" value="Next" onClick="survey_handle()"></td><td>';
div_content+='<input type="button" class="btn" value="Quit" onClick="quit_survey()"></td></tr>';
div_content+="</table></br>";
$("#survey_holder").html(div_content);
//return Success;
}
function updateProgress()
{
var progress = (loc/survey.length)*100;
$('.progress-bar').css('width', progress+'%').attr('aria-valuenow', progress);
$("#active-bar").html(Math.ceil(progress));
}
function survey_handle()
{
if(loc==0)
{
verify_captcha();
$("#message").hide();
//Save the participant data and start showing survey
fetch_demographics();
block_by_block();
updateProgress();
$('#select_message').show();
}
else if(loc<survey.length)
{
block_by_block();
updateProgress();
}
else if(loc == survey.length)
{
//Save your data and show final page
$('#select_message').hide();
survey_choices += $('input[name=block_child'+(loc-1)+']:checked', '#listform').val()+'\t';
//alert(survey_choices);
//Great way to call AJAX
save_survey(1);
}
loc++;
return false;
}
</script>
<script type="text/javascript">
window.onbeforeunload = function() {
var timeSpentMilliseconds = new Date().getTime() - startTime;
var t = timeSpentMilliseconds / 1000 / 60;
//fetch_demographics();
save_survey(0);
return "You have spent "+Math.ceil(t)+ " minute/s on the survey!";
//!!delete last inserted element if not quit
}
</script>
$(文档).ready(函数(){
$(“#选择_消息”).hide();
startTime=新日期().getTime();
});
loc=0;
block_size={{block_size}};
sid={{sid}};
调查={survey | tojson}};
调查选择=“”;
起始时间=0;
人口统计_content={};
功能保存调查(sf)
{
var timespentmillizes=new Date().getTime()-startTime;
var t=时间间隔毫秒/1000/60;
var surveydat=“”;
如果(sf==1)
{//成功
surveydat='sid='+sid+'&dem='+JSON.stringify(人口统计内容)+'&loc='+loc+'&t='+t+'&survey\'u choice='+JSON.stringify(调查选项);
}
如果(sf==0)
{//失败
surveydat='sid='+sid+'&dem='+json_编码(人口统计内容)+'&loc='+loc+'&t='+t+'&survey_选择='+json_编码(调查选择);
}
//调查保存呼叫
$.ajax({
键入:“POST”,
url:'/save_surveyresponse/'+sf,
数据:surveydat,
beforeSend:function(){
//这是我们附加加载图像的地方
$(“#调查持有人”).html(“”);
},
成功:功能(数据){
//成功的请求;对数据进行处理
$(“#ajax面板”).empty();
$('survey#u holder').html('Success');
警报(“开发警报:所有调查都已结束!立即保存数据…”);
window.location.replace('http://localhost:5000/surveys/thankyou');
},
错误:函数(){
//请求失败;向用户提供反馈
$(“#调查持有者”).html(“哎呀!过一会儿再试一次。
”);
}
});
}
函数验证\验证码()
{
//警报($('#g-recaptcha-response').html());
}
功能块×功能块()
{
var div_内容=“”;
var ii=0;
var区块=测量[loc];
var temp_数组=block.split(“:”);
如果(loc>=1)
{
var radio_val=$('input[name=block_child'+(loc-1)+']:checked','#listform').val();
//控制台日志(无线电值);
if(无线电值!=未定义)
调查选项+=无线电值+'\t';
其他的
{
警告(“请选择其中一个选项”);
loc——;
返回false;
}
}
对于(ii=0;ii我确定了问题所在。我正在使用json_encode而不是json.stringify,因此它正在崩溃(我在sf=1案例中发现并更改了它)。调试器的提示非常宝贵。而且,即使没有async:false,它也可以正常工作
再次感谢@AdrianoRepetti!我确定了问题所在。我正在使用json_encode而不是json.stringify,因此它正在崩溃(我在sf=1案例中发现并更改了它)。调试器的提示非常宝贵。而且,即使没有async:false,它也可以正常工作
再次感谢@AdrianoRepetti!你的浏览器调试器怎么说?可能是@AdrianoRepetti的重复,我刚刚用“async:false”尝试过,但仍然是一样的。我问这个问题是因为它以前工作得很好(在onbeforeunload中调用save_survey())。在我在save_survey()中做了几次编辑之后函数,它停止工作。@Strukt当调试器随选项卡关闭时,我没有机会看到调试器。我正在使用inspect元素捕获控制台。如果操作错误,请纠正我。@Strukt我正在使用Flask,但在服务器控制台中看不到任何请求。您的浏览器调试器怎么说?可能与@AdrianoRe重复petti我刚刚尝试过“async:false”,但还是一样。我问这个问题是因为它以前工作得很好(在onbeforeunload中调用save_survey())。在我在save_survey()中做了几次编辑之后函数,它停止工作。@Strukt当调试器随选项卡关闭时,我没有机会看到调试器。我正在使用inspect元素捕获控制台。如果操作错误,请纠正我。@Strukt我正在使用Flask,在服务器控制台中没有看到任何请求。