How can I get the exact height and width of the currently open browser screen window?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use Javascript to get the viewport width and height. Then pass the values back via a hidden form input or ajax.
At its simplest
Complete method using hidden form inputs
Assuming you have: JQuery framework.
First, add these hidden form inputs to store the width and height until postback.
Next we want to get the window (viewport) width and height. JQuery has two methods for this, aptly named width() and height().
Add the following code to your .aspx file within the head element.
Result
This will result in the width and height of the browser window being available on postback. Just access the hidden form inputs like this:
This method provides the height and width upon postback, but not on the intial page load.
Note on UpdatePanels: If you are posting back via UpdatePanels, I believe the hidden inputs need to be within the UpdatePanel.
Alternatively you can post back the values via an ajax call. This is useful if you want to react to window resizing.
Update for jquery 3.1.1
I had to change the JavaScript to: