Jump to content

JavaScript:Tutorial, Screen Resolution

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
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



#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
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:

#3
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

Sidewinder said:

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 ;)

Quote

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 ;)

Quote

again dont laugh at me, i know nothing about JS :cool:
Well I have nothing to laugh to.. thats why I make tutorials ;)

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Nice tutorial! Thanks!

#5
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Thanks :)
Sidewinder did my post help you?

#6
xtraze

xtraze

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 910 posts
Really great tutorial and I am sure I am going to use this, thank you for sharing it.

#7
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

xtraze said:

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!

#8
mkamoski

mkamoski

    Newbie

  • Members
  • Pip
  • 1 posts

TcM said:

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