0

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?

4
  • 2
    you are mixing PHP, Java, JavaScript and CSS. None of this will work. Commented Sep 29, 2012 at 13:25
  • have you considered using jQuery? Commented Sep 29, 2012 at 13:26
  • Isn't this is what CSS media-queries are for? Commented 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. Commented Sep 29, 2012 at 15:31

1 Answer 1

3

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
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Should inputting the first solution into my above function work if inserted correctly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.