Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, December 01, 2011

Get Monitor Screen Resolution with Javascript

Find out and display a user's screen resolution with javascript.

In javascript, the screen.width and screen.height properties contain
the size a visitor's monitor is set to. Bear in mind that the size the
monitor is set to is not the same as the size of the browser window a
visitor is using - windows can of course be set to different sizes. To
find out this information you need to check the height and width of
the viewport (different methods for different browsers,
unfortunately).

Your current screen resolution (if you have javascript enabled) is
1280x1024

To display a visitor's screen resolution, simple include the following
code in your page:

<script type="text/javascript">
document.write(screen.width+'x'+screen.height);
</script>

Screen resolution redirect

Once you know what a visitor's screen resolution is, you can redirect
them to a particular page. Admittedly, you'd be better off with a page
that worked at any screen resolution, but anyhow ;)

The script below will redirect users based on whether or not they have
a monitor resolution of 800x600 or below:

<script type="text/javascript">
if ((screen.width<=800) && (screen.height<=600)) {
 window.location.replace('http://example.com/800-600-or-less');

}
else {
   window.location.replace('http://example.com/greater-than-800-600');
}
</script>

No comments:

Post a Comment