Created
December 12, 2012 01:20
-
-
Save zastrow/4264023 to your computer and use it in GitHub Desktop.
Script to display window dimension. Updates on resize for getting needed measurements for RWD media queries. Library independent.
This file contains hidden or 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
// Window Dimensions | |
window.onload = function() { | |
makeDiv(); | |
showWin(); | |
}; | |
window.onresize = showWin; | |
function makeDiv() { | |
var d = document.createElement('div'); | |
d.id = 'winDim'; | |
d.setAttribute('style','position:fixed;z-index:100000000;right:0;bottom:0;padding:4px 8px;color:#999;font-family:monospace;background:#333;'); | |
document.body.insertBefore(d, document.body.firstChild); | |
}; | |
function showWin() { | |
var w = window.innerWidth, | |
h = window.innerHeight; | |
document.getElementById("winDim").innerHTML = w + "px × " + h + "px"; | |
}; |
My first attempt killed <body>
content. So, I stopped that.
Also, I added a z-index
of 100,000,000. The window info should stay layered above everything. You can always add more zeros.
Check out a working sample at CodePen.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested and works in Safari and Chrome on Mac and Internet Explorer 9.
This script doesn't work in IE8.