Your answers are great! How did you learn so much?
Got things mostly working with your last suggestions only to discover that some of the pages that are returned by that rqserver.exe file contain frames. Only like, 2 or 3 of them. (There are a number of pages generated by that cgi file). So, my question is, do you know of a way for the idle detect to work on a frame inside an iframe?
Thanks so much!
(One of my bosses is originally from the Philippines too, as I see it says you are, I think that's neat )
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
This will idle detect in each iframe and all its sub-iframes and sub-frames up to the innermost frame/iframe, if any.
Change the detectIdle function with the below function and add the setWinHandlers and xAttachEvent functions. I made a cross-browser event registration that will not override the events set in these codes if those are already registered inside the frames/iframes.
Code:
function detectIdle(win){
if (!win) win = window;
setWinHandlers(win);
//idle detect for each iframe inside each iframe
var iframes = win.document.getElementsByTagName('iframe');
for (var i=0; i<iframes.length; i++){
win = iframes[i].contentWindow;
setWinHandlers(win);
detectIdle(win);
}
//idle detect for each frame inside each frameset
var frames = win.frames;
for (var i=0; i<frames.length; i++){
win = frames[i];
setWinHandlers(win);
detectIdle(win);
}
}
function setWinHandlers(win){
xAttachEvent(win.document, 'mousemove', initRedirect);
xAttachEvent(win.document, 'click', initRedirect);
xAttachEvent(win.document, 'keydown', initRedirect);
xAttachEvent(win, 'resize', initRedirect);
}
function xAttachEvent(el, evtName, handler){
if (el.addEventListener){
el.addEventListener(evtName, handler, false);
}
else if (el.attachEvent){
el.attachEvent('on' + evtName, handler);
}
}
I feel bad asking so many questions! But I replaced the previous detectIdle(win) function with the new one you posted here, as well as adding the other two functions and it still is not detecting mouse movement when there are frames involved in the document that is inside the iframe. If there are no frames it works perfectly. But if there are frames I can only get the timer to reset if I move the scrollbar or let the mouse hover a moment over the division between frames. I've read through your code but I am not even quite sure how to go about troubleshooting it. Thanks again so much for your help!
--------From my boss---------
He says "mabuti" and "Taga saan ka sa Pinas?"
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
You mean when you move your mouse over the document inside the frames, the timer is not reset? And it only works when you move the scrollbars and mouse over the frame border? How about when you click on the document, hit any key while on the document and resize the frame? What browser are you using? I tried it in Firefox 2 and IE7 and it works.
Ok, in Firefox I tried resizing, clicking, typing and moving the mouse. Resizing would reset the timer but none of the others would.
In IE7, not even resizing worked to reset the timer.
(Hopefully I am including the code from the .js file correctly in this post)
Code:
/*This javascript file is from glennv elite coder of www.codingforums.com*/
/**********************************************************/
/* This function sets and clears timers in the detection */
/* of mouse activity. */
/**********************************************************/
var xScroll, yScroll, timerPoll, timerRedirect, timerClock;
function initRedirect(){
if (typeof document.body.scrollTop != "undefined"){ //IE,NS7,Moz
xScroll = document.body.scrollLeft;
yScroll = document.body.scrollTop;
clearInterval(timerPoll); //stop polling scroll move
clearInterval(timerRedirect); //stop timed redirect
timerPoll = setInterval("pollActivity()",1); //poll scrolling
timerRedirect = setInterval("showWarning()",10000); //set timed redirect
//for tracking only
clearInterval(timerClock);
document.getElementById("clock").innerHTML="0";
timerClock=setInterval("document.getElementById('clock').innerHTML=parseInt(document.getElementById('clock').innerHTML,10)+1",1000);
//end tracking
}
else if (typeof window.pageYOffset != "undefined"){ //other browsers that support pageYOffset/pageXOffset instead
xScroll = window.pageXOffset;
yScroll = window.pageYOffset;
clearInterval(timerPoll); //stop polling scroll move
clearInterval(timerRedirect); //stop timed redirect
timerPoll = setInterval("pollActivity()",1); //poll scrolling
timerRedirect = setInterval("showWarning()",10000); //set timed redirect
//for tracking only
clearInterval(timerClock);
document.getElementById("clock").innerHTML="0";
timerClock=setInterval("document.getElementById('clock').innerHTML=parseInt(document.getElementById('clock').innerHTML,10)+1",1000);
//end tracking
}
//else do nothing
}
/**********************************************************/
/* This function checks for activity on the screen. */
/**********************************************************/
function pollActivity()
{
if ((typeof document.body.scrollTop != "undefined" && (xScroll!=document.body.scrollLeft ||
yScroll!=document.body.scrollTop)) //IE/NS7/Moz
||
(typeof window.pageYOffset != "undefined" && (xScroll!=window.pageXOffset || yScroll!=window.pageYOffset))) //other browsers
{
initRedirect(); //reset polling scroll position
}
}
/**********************************************************/
/* This will idle detect in each iframe and all its */
/* sub-iframes and sub-frames up to the innermost */
/* frame/iframe, if any */
/**********************************************************/
function detectIdle(win){
if (!win) win = window;
setWinHandlers(win);
//idle detect for each iframe inside each iframe
var iframes = win.document.getElementsByTagName('iframe');
for (var i=0; i<iframes.length; i++){
win = iframes[i].contentWindow;
setWinHandlers(win);
detectIdle(win);
}
//idle detect for each frame inside each frameset
var frames = win.frames;
for (var i=0; i<frames.length; i++){
win = frames[i];
setWinHandlers(win);
detectIdle(win);
}
}
function setWinHandlers(win){
xAttachEvent(win.document, 'mousemove', initRedirect);
xAttachEvent(win.document, 'click', initRedirect);
xAttachEvent(win.document, 'keydown', initRedirect);
xAttachEvent(win, 'resize', initRedirect);
}
function xAttachEvent(el, evtName, handler){
if (el.addEventListener){
el.addEventListener(evtName, handler, false);
}
else if (el.attachEvent){
el.attachEvent('on' + evtName, handler);
}
}
/**********************************************************/
/* This function shows the warning by calling toggleLayer */
/* and setting a timer to redirect to the lougout page. */
/**********************************************************/
function showWarning()
{
toggleLayer('mysvcwarning');
clearInterval(timerRedirect); //stop timed redirect
timerRedirect = setInterval("location.href='https://secure.skagit.edu/login/logout.asp'",20000); //set timed redirect
}
/**********************************************************/
/* This javascript file I swiped from James (the SVC */
/* webmaster) to toggle the visibility of a div layer */
/**********************************************************/
function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
// this line sets the focus to the link in the warning layer
document.getElementById('warning').focus();
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
// this line sets the focus to the link in the warning layer
document.all['warning'].focus();
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}
And then this is the file that uses the idledetect.js file
Code:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<LINK rel="stylesheet" type="text/css" href="includes/style.css" />
<script src="includes/idledetect.js" type="text/javascript"></script>
</head>
</head>
<body>
Timer: <span id="clock" style="font-weight:bold">0</span>
<!--Container for STSOS pages-->
<iframe id="mainFrame" name="mainFrame" width="100%" height="100%" src="default_new.asp"
onload="detectIdle(this.contentWindow);"
align="middle" frameborder="0" marginheight="0" marginwidth="0"
vspace="0" hspace="0" style="visibility:visible">
</iframe>
<!-- Warning div layer -->
<div id="mysvcwarning">
<p style="text-align:center; font-family:arial, verdana, sans-serif; font-size:0.75em;">
This is a reminder to keep your records secure.<br />
<a id="warning" href="/login/logout2.asp" target="_parent" onblur="toggleLayer('mysvcwarning')">
Click here to clear your records if you are done,</a>
<br />
or click on the main window to continue.
</p>
</div>
</body>
</html>
-----From my boss-----
"Ala eh Kumusta Kabayan sa Quezon ang boss ko"
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
The HTML must be in standards-compliant mode causing document.body.scrollTop to always return 0. document.documentElement must be used instead of document.body when in this mode.
To solve, change the pollActivity function to this:
Glenngv, your help has been greatly appreciated. If you are still around and still willing to answer one more question on this, it is almost working (I got side-tracked by other projects and this one was put on hold).
If you go to https://secure.skagit.edu/idledetect.htm you will see it with the clock ticking at the top (IE7 was awful to try and get the iframe to take up the whole page but I finally got it to work). The problem is when you click on the links where it says "Live chat with SVC:". That splits the window into two frames and works until you have entered your name and subject and then click next. After clicking next typing nor mouse movement in the chat window stops the timer but if you move the mouse to the other part of the window, it will stop the timer.
How to detect idle on each iframe and also on the main page?
Dear glenngv,
I have 2 iframes in the main page. How I can detect idle on each iframe and also on the main page together? Could you mock up a complete file so I can test it as I try to modify the previous code you post before but fail to do so. (I am weak in js issue )