I have a web Application running on a controller (Scarcely limited processing, memory and network bandwidth). The page is basically a simple HTML file full of LEDs that should be updated on an interval. On each interval, Javascript sends an Ajax request to the server and updates all the LEDs based on the reply. Now this works fine!
The problem is when the user opens one of these pages and start browsing other stuff. For security and economical reasons, we don't want to update the page when the client is not seeing this page. This is the algorithm:
I developed this little test code to see how page events work (see live on jsFiddle):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function m(msg){
document.getElementById("logplace").innerHTML+=msg+"<br/>";
}
</script>
</head>
<body onblur="m('Blur')" onerror="m('Error')" onfocus="m('Focus')" onload="m('Load')" onresize="m('Resize')" onunload="m('Unload')">
<span id="logplace"></span>
</body>
</html>
I need to page to be only updated if the user is currently viewing it. I searched the Stackoverflow and the following threads don't seem to be the answer:
- Keep track of which tab was being viewed when page is refreshed
- How to run multiple ajax calls on one page
- Function repeating when page not being viewed
PS. JQuery exists on the server as well. But if there is a simpler way, I prefer not to use JQuery.
I have a web Application running on a controller (Scarcely limited processing, memory and network bandwidth). The page is basically a simple HTML file full of LEDs that should be updated on an interval. On each interval, Javascript sends an Ajax request to the server and updates all the LEDs based on the reply. Now this works fine!
The problem is when the user opens one of these pages and start browsing other stuff. For security and economical reasons, we don't want to update the page when the client is not seeing this page. This is the algorithm:
I developed this little test code to see how page events work (see live on jsFiddle):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function m(msg){
document.getElementById("logplace").innerHTML+=msg+"<br/>";
}
</script>
</head>
<body onblur="m('Blur')" onerror="m('Error')" onfocus="m('Focus')" onload="m('Load')" onresize="m('Resize')" onunload="m('Unload')">
<span id="logplace"></span>
</body>
</html>
I need to page to be only updated if the user is currently viewing it. I searched the Stackoverflow and the following threads don't seem to be the answer:
- Keep track of which tab was being viewed when page is refreshed
- How to run multiple ajax calls on one page
- Function repeating when page not being viewed
PS. JQuery exists on the server as well. But if there is a simpler way, I prefer not to use JQuery.
Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Feb 7, 2012 at 9:46 AlexStackAlexStack 17.5k22 gold badges75 silver badges107 bronze badges 3- Didn't ask the right question. Is this page being viewed is teh key bit. Answer are various and potentially iffy though. Try stackoverflow./questions/354718/… – Tony Hopkinson Commented Feb 7, 2012 at 10:06
- The question from your link is about a page element having focus. I'm asking about the entire page being viewed or not. – AlexStack Commented Feb 7, 2012 at 10:28
- The page is an element, so same sort of issue.... – Tony Hopkinson Commented Feb 7, 2012 at 14:37
3 Answers
Reset to default 4var focused = false;
function onBlur() {
focused = false;
};
function onFocus(){
focused = true;
};
if (/*@cc_on!@*/false) { // check for Internet Explorer
document.onfocusin = onFocus;
document.onfocusout = onBlur;
} else {
window.onfocus = onFocus;
window.onblur = onBlur;
}
Then your javascript can check
if (focused)
{
// process ajax
}
source: http://www.thefutureoftheweb./blog/detect-browser-window-focus
Check out the demo he created so you can see it in action: http://www.thefutureoftheweb./demo/2007-05-16-detect-browser-window-focus/
With that in mind, if your page uses an interval timer to update, when the window loses focus, you could kill the interval, then start it again when the window is focused.
As far as I can see you have the page list in your db. You can add a field called is_viewed and update it by +1 each time users enters the page and -1 each time he exits the page. So if it is ==0 you can perform your check.
I don't see any other way (not Ajax) to check what is going on another page. So you'll have to send the request anyway
I propose that when you start the window (that you want to update only when visible) you start it from a master window, or the other window that has focus. Combine the focus solution by Francis and the following technique whereby you can access the DOM of the window from another.
You can look here for more information. Basically you will use the following code
var pageControl=open('page.html','page name','width=200,height=200')
to open the page and be able to access it via the pageControl
variable using javascript.
It solves the problem by allowing you to update the page when either windows has focus, controlling the updates from the other window or from a master window. The only potential roadblock from here would be patibility.
A master window solution may be a little more involved. But if you were to do this from the other window you could imagine that if you find no window has focus then you wouldn't fire the update script.. otherwise fire it!