Cross-Browser Snippet for finding the size of the browser window using JavaScript

Advertisement

To start with, Well I think bunch of developers might have come across this situations where in which they need to know the ‘width’ & ‘height’ of the browser window. For finding the size of the browser window people will be writing a simple code like this “window.innerHeight/Width” this will work for most of the browsers but not Internet Explorer. In order to overcome this sort of situations we have the snippet code which has got simple code which is a Cross-Browser Snippet for finding the size of the browser window. I have tested in Mozilla FireFox and Internet Explorer works well enough.

function browserWindowSize() {
var browserWinWidth = 0, browserWinHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
browserWinWidth = window.innerWidth;
browserWinHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
browserWinWidth = document.documentElement.clientWidth;
browserWinHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
browserWinWidth = document.body.clientWidth;
browserWinHeight = document.body.clientHeight;
}
alert( 'Browser Window Width = ' + browserWinWidth +' Browser Window Height = '+browserWinHeight);
}

Guys, Hope this will help some of the developers who wants to know the size of the browser window. So what are you waiting for… just enjoying coding…

Related Entries...

World News:

8 Responses

05.13.07

Thanks!

05.13.07

doesn’t work with IE7

05.13.07

Hi Aug, Firstly I would like to thank you! for posting your valuable comments, this code works fine with Internet Explorer 7 also. Could you please check once again.

05.13.07

Will it work across all browsers, would be nice if you could show where to insert the code like after

05.13.07

Yes this code works fine in all the browsers

05.13.07

Yeah, thanks. Just what I needed after a long search

05.13.07

I have Win XP – IE7, FF3, Safari & Opera, The function Works like charm

aug didn’t do his homework, I guess that the did not even call the function.

I mean if you wanna try, dont forget to call the function like this:

browserWindowSize();

05.13.07

Exactly, we have to call the function like you have mentioned – browserWindowSize()… :)

Leave Your Response

* Name, Email, Comment are Required

Online Sponsors

Start Foreign Exchange Trading today with the Forex Affiliate Program and also you can have access Online Forex News to know what happens every single moment.


Forex Floor