getViewport

D

Dean A. Hoover

I just wrote the following code to get the "viewport"
in the browser. Its the x, y, width, and height in
the scroll view. I only have IE 6 and Opera 7 on
windows 2000, and Mozilla 1.72 on linux. On those
platforms the code appears to work. Any suggestions
on how to ensure that it works on other platforms?

Dean Hoover

function getViewport()
{
var viewport = {x:0,y:0,width:0,height:0};

if (typeof window.scrollX != 'undefined')
{
viewport.x = window.pageXOffset;
viewport.y = window.pageYOffset;
viewport.width = window.innerWidth;
viewport.height = window.innerHeight;
return viewport;
}
else
{
if (document.documentElement &&
(typeof document.documentElement.scrollLeft != 'undefined') &&
(document.documentElement.scrollLeft != 0))
{
viewport.x = document.documentElement.scrollLeft;
viewport.y = document.documentElement.scrollTop;
viewport.width = document.documentElement.clientWidth;
viewport.height = document.documentElement.clientHeight;
return viewport;
}
else
{
if (document.body &&
(typeof document.body.scrollLeft != 'undefined'))
{
viewport.x = document.body.scrollLeft;
viewport.y = document.body.scrollTop;

if (document.compatMode == "CSS1Compat")
{
viewport.width = document.body.parentNode.clientWidth;
viewport.height = document.body.parentNode.clientHeight;
}
else
{
viewport.width = document.body.clientWidth;
viewport.height = document.body.clientHeight;
}

return viewport;
}
}
}

return null;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top