Created
May 21, 2012 08:28
-
-
Save alexeypegov/2761166 to your computer and use it in GitHub Desktop.
Publish event each time browser window is focused/blurred or resized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(["class", "pubsub"], function(Class, Pubsub) { | |
var BrowserUtil = Class.extend({ | |
init: function() { | |
var blur = function onBlur() { | |
document.body.className = 'blurred'; | |
Pubsub.publish("window:blurred") | |
}; | |
var gain = function onFocus(){ | |
document.body.className = 'focused'; | |
Pubsub.publish("window:focused") | |
}; | |
if (BrowserUtil.isIE()) { | |
document.onfocusin = gain; | |
document.onfocusout = blur; | |
} else { | |
window.onfocus = gain; | |
window.onblur = blur; | |
} | |
window.onresize = function(e) { | |
if (window._resize) { | |
clearTimeout(window._resize); | |
} | |
window._resize = setTimeout(function() { | |
delete window._resize; | |
Pubsub.publish("window:resized", e); | |
}, 300); | |
}; | |
} | |
}); | |
BrowserUtil.isIE = function() { | |
if (window._isIE === undefined) { | |
var div = document.createElement('div'); | |
div.innerHTML = '<!--[if IE]><i></i><![endif]-->'; | |
window._isIE = (div.getElementsByTagName('i').length === 1); | |
} | |
return window._isIE; | |
}; | |
return BrowserUtil; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment