Thursday, April 12, 2007

IE JS hack :Catching the Maximise / Minimise event

Here's a little hack to find out whether the user has maximised or minimised the window .This can then be used to trigger other useful chain of events of your choice .

Explanation:
The screenX, and screenY properties of the event corresponds to the the x and y coordinates of the mouse pointer's position relative to the user's screen respectively .

Now comes the juicy part ....

IE forces a mouse move whenever you change the focus of the window .So the interesting point to note is that - a mousemove event is forced even though the mouse co-ordinates dont actaully move ! So if you are able to track the previous mouse co-ords and catch the point where a mouse move is called - but at the same time the values dont change - bingo ! 8 )




var logs ,x=-1, y=-1 ,ctr = 2;
document.onmousemove = catchmaxmin;

function catchmaxmin(ev){
logs = document.getElementById('logs');
var e = (ev!=undefined)?ev:event;
if(e["screenX"]==x && e["screenY"]==y && ctr%2==0 ){
logs.innerHTML+= '
maximized /minimized ! ';
}
ctr++;
x = e["screenX"];
y = e["screenY"];
}


I've been looking for a similiar hack in firefox ,but i guess this useful bug has'nt been introduced into the Mozilla camp . 8 ) Here's a working demo ( tested on ie6 , ie7 ) of the 10 odd lines of code needed .

Keep Clicking,
Bhasker V Kode

Technorati Tags: , , , , , , , ,

2 comments:

Agnel said...

hey, the demo isn't working man.
What hap?
-Jude

Bosky said...

when i said IE , imeant it works only on internet explorer .

8 )

Keep Clicking,
Bhasker