【发布时间】:2014-06-10 00:23:39
【问题描述】:
我有一个会话超时脚本,现在已经在我的网站上成功运行了很多个月。脚本来自http://www.jensbits.com/2010/04/18/coldfusion-session-timeout-with-warning-and-session-refresh/
突然之间,它抛出以下错误:
未捕获的错误:无法在初始化之前调用对话框上的方法;试图调用方法“打开”
这是我的脚本:
<script type="text/javascript" language="javascript">
$(function(){
// jQuery UI Dialog
$('#dialogWarning').dialog({
autoOpen: false,
width: 400,
modal: true,
resizable: false,
buttons: {
"Restart Session": function() {
//reset session on server
$.post("restart_session.cfm");
//reset the variables
requestTime = new Date();
warningStarted = false;
countdownTime = warning;
//clear current checkSessionTimeEvent and start a new one
clearInterval(checkSessionTimeEvent);
checkSessionTimeEvent = "";
checkSessionTimeEvent = setInterval("checkSessionTime(requestTime)",warning*1000);
$('#dialogWarning').dialog('close');
}
}
});
$('#dialogExpired').dialog({
autoOpen: false,
width: 400,
modal: true,
resizable: false,
close: function() {
window.location="https://<cfoutput>#subscriber.hostName#.#subscriber.baseURL#</cfoutput>/serviceticket/index.cfm?error=2";
},
buttons: {
"Login": function() {
window.location="https://<cfoutput>#subscriber.hostName#.#subscriber.baseURL#</cfoutput>/serviceticket/index.cfm?error=2"; }
}
});
});
//Your timing variables in number of seconds
//total length of session in seconds 5400 - 1800
var sessionLength = 60;
//time warning shown (10 = warning box shown 10 seconds before session starts) 180
var warning = 10;
//time redirect forced (10 = redirect forced 10 seconds after session ends) 0
var forceRedirect = 0;
$(document).ready(function() {
//event to check session time left (times 1000 to convert seconds to milliseconds)
checkSessionTimeEvent = setInterval("checkSessionTime(requestTime)",warning*1000);
});
//event to check session time variable declaration
var checkSessionTimeEvent = "";
//time session started
var requestTime = new Date();
//initial set of number of seconds to count down from for countdown ticker (10,9,8,7...you get the idea)
var countdownTime = warning;
//create event to start/stop countdownTicker
var countdownTickerEvent = "";
//initially set to false. if true - warning dialog open; countdown underway
var warningStarted = false;
function checkSessionTime(reqTime)
{
//get time now
var timeNow = new Date();
//clear any countdownTickerEvents that may be running
clearInterval(countdownTickerEvent);
//difference between time now and time session started variable declartion
var timeDifference = 0;
//session timeout length
var timeoutLength = sessionLength*1000;
//set time for first warning, ten seconds before session expires
var warningTime = timeoutLength - (warning*1000);
//force redirect to log in page length (session timeout plus 10 seconds)
var forceRedirectLength = timeoutLength + (forceRedirect*1000);
timeDifference = timeNow - reqTime;
if (timeDifference > warningTime && warningStarted === false)
{
//reset number of seconds to count down from for countdown ticker
countdownTime = warning;
//call now for initial dialog box text (time left until session timeout)
countdownTicker();
//set as interval event to countdown seconds to session timeout
countdownTickerEvent = setInterval("countdownTicker()", 1000);
$('#dialogWarning').dialog('open');
warningStarted = true;
}
else if (timeDifference > timeoutLength)
{
//close warning dialog box if open
if ($('#dialogWarning').dialog('isOpen')) $('#dialogWarning').dialog('close');
$('#dialogExpired').dialog('open');
}
if (timeDifference > forceRedirectLength)
{
//clear (stop) checksession event
clearInterval(checkSessionTimeEvent);
//force relocation
window.location="https://<cfoutput>#subscriber.hostName#.#subscriber.baseURL#</cfoutput>/serviceticket/index.cfm?error=2"; }
}
function countdownTicker()
{
//put countdown time left in dialog box for 10, 9, 8...
$("span#dialogText-warning").html(countdownTime);
//decrement countdownTime
countdownTime--;
}
</script>
我正在运行以下 JQuery 版本:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.0/jquery.mobile.min.css" />
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
【问题讨论】:
-
您是否想弄清楚它开始失败的原因?或者您知道为什么它开始失败(由于升级/软件更改)并试图克服这个错误吗?
-
为什么标记为
ColdFusion?似乎这严格来说是一个 jQuery 问题。 -
好吧,我绝对不知道它为什么会失败。自脚本实施以来所做的唯一更改是在页面中添加了一个 jquery 选项卡元素。我刚刚测试了该脚本是否可以在没有 jquery 选项卡的页面上运行。所以要回答你的问题,两者都是。
-
ColdFusion 标签已移除。
-
很奇怪。我在您的代码中看不到任何允许在对话框启动之前尝试修改对话框的内容。副主题,您应该对脚本和样式进行分组,以便它们可以并行加载。换句话说,将
<link>上移 1 行,在 jquery 包含上方。
标签: jquery