spacer
spacerLokken Logo
spacerLokken Binary Logo
spacer
spacer
spacer
spacer spacer spacer spacer
Determine Window Focus
Language:   Client Side - JavaScript
Author:   Jon Lokken
Date:   6-29-2005

Description: View Example
This is used to show the focused window on a group of windows.

<script type="text/javascript">
var myFocus = true;
var children = Array();
var statusMessage = "";

window.onload = init;
window.onfocus = doFocus;
window.onbeforeunload = confirmExit;

function confirmExit(){
var alertValue = window.confirm("Are you sure?");
if(!alertValue){
return false;
}
}
function init(){
setTimeout("myDetails()", 5000);
}
function doFocus(){
myFocus = true;
// Unfocus my children and all of their Children;
unfocusChildren();
// Unfocus my parent and all brothers and sisters;
unfocusParent();
}

function unfocusParent(){
try{
var parent = window.opener;
if(!parent.closed){
parent.myFocus = false;
parent.unfocusBrothers(window.name);
parent.unfocusParent();
}
} catch(err) {
// Do Nothing;
}
}
function unfocusBrothers(myWindowName){
var win2Name = myWindowName
for(var i=0;i<children.length;i++){
if((!children[i].closed) && (children[i].name != win2Name)){
children[i].myFocus = false;
children[i].unfocusChildren();
}
}
}
function unfocusChildren(){
for(var i=0;i<children.length;i++){
if((!children[i].closed)){
children[i].myFocus = false;
children[i].unfocusChildren();
}
}
}

function openNewWindow(strPageName, title){
var strAttrib ="height=400,width=500,ScreenX=0,screenY=400,left=0,top=0,resizable,
toolbar=no,menubar=no,location=no,scrollbars=yes,status=no";
var today = new Date();
var millisec = today.getMilliseconds();
var strWinRef = "title" + millisec;
children[children.length++] = window.open(strPageName, strWinRef, strAttrib);
myFocus = false;
}

function myDetails(){
statusMessage = statusMessage + "--------------------------<br>";
statusMessage = statusMessage + "My Details...<br>";
statusMessage = statusMessage + "My Name:"+window.name+"<br>";
statusMessage = statusMessage + "My Focus:"+myFocus+"<br>";
listMyParent();
listMyChildren();
document.getElementById("childList").innerHTML = statusMessage +document.getElementById("childList").innerHTML;
statusMessage = "";
setTimeout("myDetails()", 5000);
}
function listMyParent(){
statusMessage = statusMessage + "Listing Parent...<br>";
try{
if(!window.opener.closed){
statusMessage = statusMessage + "Parent:"+window.opener.name+"<br>";
}
} catch(err) {
statusMessage = statusMessage + "Parent:None<br>";
}
}
function listMyChildren(){
statusMessage = statusMessage + "Listing Children...<br>";
if(children.length == 0){
statusMessage = statusMessage + "Children:None<br>";
} else {
for(var i=0;i<children.length;i++){
if(!children[i].closed){
statusMessage = statusMessage +"Child Window ("+i+")("+children[i].name+") hasfocus?"+children[i].myFocus+"<br>";
}
}
}
}
</script>


Disclaimer:
Feel free to use this code I have written.  I only ask that you leave my name in the comments.  I take no responsibility for this code working, imply no warranty as to it's validity, and will not be held liable if you decide to try it.  I am only trying to help out others so they don't have to struggle with the same problems as me...

Associated Sites: Jon Lokken - http://www.jonlokken.com | Amy Lokken - http://www.amylokken.com | Isabel Lokken - http://www.isabellokken.com | Elizabeth Lokken - http://www.elizabethlokken.com | Lokken.us - http://www.lokken.us | Lokken.co - http://www.lokken.co | Lokken Consulting - http://www.lokkenconsulting.com
Author: Jon Lokken - http://www.lokken.net