JavaScript to return dimensions

Three useful functions for JavaScript.

They are not the best since you need to have X and Y as global variables, but it works.

var x,y;//GLOBAL
function returnInnerDimensions(){//returns screen size X,Y
	if (self.innerHeight){ // all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight){// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body){ // other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
}
 
function returnScrollOffset(){//return how much we scrolled already
	if (self.pageYOffset){ // all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop){// Explorer 6 Strict
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body){ // all other Explorers
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
}
 
function returnPageDimensions(){//returns the page size, max X and max Y
	var sh = document.body.scrollHeight;
	var oh= document.body.offsetHeight
	if (sh > oh){ // all but Explorer Mac
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else {// Explorer Mac;would also work in Explorer 6 Strict, Mozilla and Safari
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
}

Works in ie5,ie6,ie7,ie8, Firefox, Chrome, Safari. That I could try ;)

Related Posts Related Websites
Help sharing and Flatter me ;)

Share

2 Comments

  1. very cool & good code, thank you very much for sharing.

    Can you share this code on my JavaScript library?

    Awaiting your response. Thank

  2. Chema says:

    Feel free to use it in your site ;)

Leave a Reply

Follow me