I know there are various threads on this topic. But I am wondering if my approach is entirely incorrect. I am trying to set the height of a wrapper depending on the browser size. So far I have this JavaScript function that returns the height and width of the screen.
<script language="JavaScript">
var screenW = 640, screenH = 480;
if (parseInt(navigator.appVersion)>3) {
screenW = screen.width;
screenH = screen.height;
}
else if (navigator.appName == "Netscape"
&& parseInt(navigator.appVersion)==3
&& navigator.javaEnabled()
)
{
var jToolkit = java.awt.Toolkit.getDefaultToolkit();
var jScreenSize = jToolkit.getScreenSize();
screenW = jScreenSize.width;
screenH = jScreenSize.height;
}
</script>
I wish to call the values of these variables later in :
<div id="wrapper" style = "height: valuepx; screenHwidth: screenWpx;> in similar fashion to a PHP echo.
How should I be approaching calling the variable?
Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked Sep 29, 2012 at 13:23
Ríomhaire
3,1344 gold badges27 silver badges42 bronze badges
-
2you are mixing PHP, Java, JavaScript and CSS. None of this will work.Daniel A. White– Daniel A. White2012年09月29日 13:25:35 +00:00Commented Sep 29, 2012 at 13:25
-
have you considered using jQuery?Daniel A. White– Daniel A. White2012年09月29日 13:26:01 +00:00Commented Sep 29, 2012 at 13:26
-
Isn't this is what CSS media-queries are for?ZenMaster– ZenMaster2012年09月29日 14:02:22 +00:00Commented Sep 29, 2012 at 14:02
-
Thanks for the feedback. No I have not considered it. I am new to JavaScript and CSS. What would be the best resource for me to read that should help me work this out? Thanks in advance.Ríomhaire– Ríomhaire2012年09月29日 15:31:05 +00:00Commented Sep 29, 2012 at 15:31
1 Answer 1
You have to assign the style from Javascript:
var wrapper=document.getElementById("wrapper");
wrapper.style.height = screenH;
wrapper.style.width = screenW;
or with jQuery:
$("#wrapper").css({
width:screenW,
height:screenH
})
answered Sep 29, 2012 at 13:28
John Dvorak
27.4k13 gold badges73 silver badges86 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Ríomhaire
Thanks. Should inputting the first solution into my above function work if inserted correctly?
default