Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

JavaScript:Tutorial, Screen Resolution


  • Please log in to reply
7 replies to this topic

#1 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 03 December 2006 - 10:43 AM

Introduction:-
This is a simple tutorial.. It will get the screen resolution and if you modify it a little bit you can use it to define how your website will be displayed like for example having to home pages then if the screen resolution is 1024x768 it will display index1.html and if the user has 800x600 it will display index2.html.. but then thats up to you in this tutorial I will keep it the simplest possible.

Solution:-
This goes between <head> tags:-
<script language="JavaScript">
function res() 
{
alert('Screen Resolution Is '+screen.width+' by '+screen.height);
}
</script>

And this as the body code

<body onLoad='res()'

Explanation:-
Can it be simpler than that?

A Preview:-
None

Conclusion:-
As Always Feedback is welcome and the full source is attached!!

Attached Files


  • 0

#2 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 03 December 2006 - 11:23 AM

nice, i know nothing about JavaScript but i have 2 noobie questions.

1) How would you calculate 90% of the screen width? Could you just do it like
alert('Screen Resolution Is '+screen.width*.9+' by '+screen.height);
lol?

2) Also since i guess res() returns two values would it be possable to use screen.width as like a varible? say
<table width='screen.width'><tr><td>blah></td></tr></table>

again dont laugh at me, i know nothing about JS :cool:
  • 0

#3 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 03 December 2006 - 11:56 AM

1) How would you calculate 90% of the screen width? Could you just do it like

alert('Screen Resolution Is '+screen.width*.9+' by '+screen.height);

Yes thats it ;)

2) Also since i guess res() returns two values would it be possable to use screen.width as like a varible? say

<table width='screen.width'><tr><td>blah></td></tr></table>

Yes you can use it as a variable for example:-
<body>
<script language="JavaScript">
window.open('about:blank','window name','width=screen.width,height=200');
</script>
</body>
This will open a new window with the width of the screen resolution using screen.width directly and height of 200
Or you can declare screen.width as a variable and then use the variable like this:-
<body>
<script language="JavaScript">
var abc=screen.width
window.open('about:blank','window name','width=abc,height=200');
</script>
</body>
its pretty the same ;)

again dont laugh at me, i know nothing about JS :cool:

Well I have nothing to laugh to.. thats why I make tutorials ;)
  • 0

#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 06 December 2006 - 09:49 AM

Nice tutorial! Thanks!
  • 0

#5 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 06 December 2006 - 10:17 AM

Thanks :)
Sidewinder did my post help you?
  • 0

#6 xtraze

xtraze

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 872 posts

Posted 29 December 2006 - 11:37 PM

Really great tutorial and I am sure I am going to use this, thank you for sharing it.
  • 0

#7 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 30 December 2006 - 03:45 AM

Really great tutorial and I am sure I am going to use this, thank you for sharing it.


Hope you use it :) Thats why I share code, because someone might need it!
  • 0

#8 mkamoski

mkamoski

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 01 April 2010 - 07:12 AM

This is a simple tutorial.. It will get the screen resolution...

All --

Please help.

This does not seem to work for me.

1. I set my monitor to 800x600.
2. I view in IE8 and get screen.width=800 and screen.height=600.
3. I view in FF3.6 and get screen.width=720 and screen.height=540

How can I fix this?

Please advise.

Thank you.

-- Mark Kamoski
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download