2012-02-23 31 views
0

我想爲我的應用程序創建一個自動註銷頁面。我正在使用Javascript來處理與經典的ASP計時器。我有三頁js腳本頁面,我調用js文件的主頁並使用OnLoad()啓動計時器。 4秒後彈出一個窗口,要求用戶繼續進行會話。但是當窗口彈出時,我在logout_alert.asp頁面上單擊「是」,它不會更新主頁上的計時器。我怎樣才能得到彈出窗口來更新計時器?以下是腳本。從另一頁面控制一個Javascript定時器?

感謝您的幫助提前,

弗蘭克G.

我有三個頁面。對於此示例頁1,頁2和頁面3

PAGE 1 = logout_timeout.js


下面是該頁面的腳本。

<!-- Begin 

var c=0; 
var t; 

function ClockCount(){ 

c=c+1; 
    //Show the clock value on the home page text field. 
    document.timerform.clock.value = c; 

if (c == 7) { 
    //The user never responded so we will kill the session and log them out. 
    alert("Your session has timed out you are logged out."); 
    window.location.href = "logout.asp" 
} 
else if (c == 4) { 

    //We will popup a window to let the user know there about to be logged out. 
    //This will give the user a chance to keep the session alive. 
    logoutalert = window.open("logout_alert.asp", "logoutalert", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=420,height=145,left = 465,top = 250"); 
} 

//The timer will go round and round while recalling this function. 
t = setTimeout("ClockCount()",1000); 

} 

//Call to start the timer. 
function StartClock() { 
ClockCount(); 
} 

//Called to stop the timer. 
function StopClock() { 
c=0 //Clear the timer. 
clearTimeout(t); //Stop it. 
} 

// End --> 

PAGE 2 = homepage.asp


本頁面我所說的logout_timeout.js文件。

<script src="logout_timeout.js" type="text/javascript"></script> 
<body OnLoad="StartClock()"> 
This will show our clock while its processing. 
<form id="timerform" name="timerform" method="post" action=""> 
    <input type="text" name="clock" id="clock" /> 
</form> 
Used now just for testing! 
    <input type="submit" name="continue" id="continue" value="START" onClick="StartClock()" /> 
    <input type="submit" name="continue" id="continue" value="STOP" onClick="StopClock()" /> 

PAGE 3 = logout_alert.asp


本頁面彈出通知,他們即將被註銷的用戶。

<script src="logout_timeout.js" type="text/javascript"></script> 
<body> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
<tr> 
<td class="Thread_Title">SESSION TIMEOUT Alert!</td> 
</tr> 
<tr> 
    <td width="85%" class="DataRows">Your session is about to timeout. Would you like to continue using WOTTS?</td> 
</tr> 
<tr> 
    <td></td> 
</tr> 
<tr> 
    <td></td> 
</tr> 
<tr> 
    <td><form id="timerform" name="timerform" method="post" action=""> 
<input type="text" name="clock" id="clock" /> 
</form></td> 
</tr> 
<tr> 
    <td> 
    <input type="submit" name="continue" id="continue" value="START" onClick="StartClock()" /> 
<input type="submit" name="continue" id="continue" value="STOP" onClick="StopClock()" /> </td> 
    </tr> 
    </table> 

回答

2

你錯過了一個難題:對其他窗口的引用。某些安全限制沙箱單獨的窗口對象,除非它們是從現有窗口生成的。如果您使用JavaScript的window.open()函數,那麼您的新窗口將打開並顯示打開它的父級的句柄。然後,在腳本中,您可以利用該句柄來操作對象和行爲,並在父窗口中執行函數。這將它們聯繫在一起。

有許多例子在這裏:

Can I pass a JavaScript variable to another browser window?

+0

你好感謝你的幫助。其中有計時器的窗口在框架內。這是該幀的代碼:並且該幀被稱爲「autologout」,但計時器腳本本身在一個js文件中。我在兩個窗口之間共享那個js文件,但插入它。但是,如果我在該頁面上有一個按鈕,我只能重置計時器。例如,如果我在定時器頁面上放置一個按鈕來停止它的工作時間,但是如果我告訴彈出窗口告訴定時器停止它不會。任何建議?謝謝! – 2012-02-23 07:44:52

+0

您可以使用window.opener,window.opener.frames [],top.opener.myFrameName.window.myFunction()...在父窗口(或框架)上調用函數。或者,您可以在子窗口的JavaScript,它在你傳遞給opener的引用時做到這一點,比如'stopRemoteTimer(top.opener.myFrameName);'其中你將以這種方式解決父窗口中的實際功能。當JS在其自己的沙箱中運行時,必須在一個或另一個窗口的上下文中執行所有計算和操作。 – ingyhere 2012-02-23 21:53:24

相關問題