how to get current screen resolution in javascript?

ctlo
ctlo used Ask the Experts™
on
how to get current screen resolution in javascript?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
<html>
<head>
</head>
<body>
<script language="javascript">
if(screen.width==800 && screen.height==600)
{
document.write("800*600");
}
else if(screen.width==1024 && screen.height==768)
{
document.write("1024*768");
}
else if(screen.width==1152 && screen.height==864)
{
document.write("1152*864");
}
</script>

</body>
</html>
even better sollution:


alert(screen.width + " x " + screen.height);


this will pop up an alert with the width and height. If your site is dependent on width or something, do it something like so:


if(screen.width >= 800) {
       window.location.href = "yourPage.html";
}
else {
       document.write("You need to set your resolution to 800x600 to view this site");
}


This could be usefull...

Peace
Ken
<html>
<head>
</head>
<body>

<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from old browsers that do not support JavaScript.

  var ScrnSize = "UnCommon"

  if (navigator.appVersion.indexOf("4.") != -1 && 
      navigator.appName.indexOf("Explorer") != -1) {
     ScrnSize = screen.width + "x" + screen.height;
  }
  if (navigator.appVersion.indexOf("4.") != -1 && 
      navigator.appName.indexOf("Netscape") != -1) {
     ScrnSize = screen.width + "x" + (screen.height + 19); //Netscape sees 19 pixels less on Height
  }
 
  document.write('Browser Type=' + navigator.appName + '<BR>');
  document.write('Browser Version=' + navigator.appVersion + '<BR>');
  document.write('Screen Resolution=' + screen.width + "x" + screen.height);

// End hiding -->
</SCRIPT>


</body>
</html>
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

anan, after testing some more, you are correct regarding netscape 4 not regarding the entire screen height. However, on my computer it is 26 pixels off. I'm running win XP and I'm thinking it has to do with the setup....

ctlo, this means that if you are working with width, you will be A-OK... however, if your design is height dependent, you might want to leave some play room in your numbers. In other words, instead of testing for 768, test for 740 (mine reports my screen height as 742).

Good job anan... one more question, in your final document.write, why don't you write ScmSize?
Ya I can use that, thanks Kenny
Michel PlungjanIT Expert
Top Expert 2009

Commented:
what a strange test.

Anand:
  if(window.screen) {    
     ScrnSize = screen.width + "x" + ((appName=="Netscape")?screen.height+19:screen.height);
}

Michel
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Please resolve this question?
Michel PlungjanIT Expert
Top Expert 2009

Commented:
ctlo,
No comment has been added lately (86 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Delete/No Refund

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

mplungjan
EE Cleanup Volunteer

Commented:
my comment works just fine .. so do the other 2 blokes' .. i recommend a split
Michel PlungjanIT Expert
Top Expert 2009

Commented:
cant split 50 points in 3

Commented:
cant ignore experts contribs too .. can we ?
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Hmm, in this case we have a few hundred answers to this question...
Michel PlungjanIT Expert
Top Expert 2009

Commented:
But you are right...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial