Reputation: 941
I am using ASP.NET and C#.I can able to get the document height using javascript with this.
var h = document.documentElement.offsetHeight;
Now i need this value during pageload. But the javascript will be start exicuting after pageload. So now i need to get it using server side code(C#).
Is it possible?
EDIT:
In pageload i am planning to set the height for all the tags based on the document height. So that the page will fit for all resolution.
Upvotes: 0
Views: 3411
Reputation: 42270
Realistically, whilst it is possible to do this, the approach is wrong. This kind of functionality would be far better implemented on the client side.
One thing that you should research is Responsive Web Design which aims to address such issues using correct semantic markup, CSS, and JavaScript.
You're saying that you want to make the elements fit according to the document size (assuming you mean window size...as the document will scale to the size of the elements in the first place) on page load, but what happens when a user re-sizes their screen? Then it all goes to pot again!
I recently had to address a similar issue using jQuery, so you might want to consider the following code snippet as it may help you achieve the desired result.
$(document).ready(function() {
// Will run when the document is ready.
resizeElements();
});
$(window).resize(function() {
// Will run when the window is resized.
resizeElements();
});
function resizeElements() {
// Write your code here to adjust the position, height and width of the desired elements
}
Upvotes: 0
Reputation: 21979
You can't get this value using server-side code, as it is, by definition, server-side.
You could however use JavaScript to pass the value back to an ASP.NET script via AJAX, and take whatever action you need to take at this point. Of course this means that you wouldn't be able to, for example, provide different content onload for different screen sizes, but it all depends on your intentions.
Since you need it on pageload, one very very hacky way to do this would be to detect the browser height and then redirect, for example pass it as a querystring value that you can use directly within PageLoad. What I mean specifically is, use JavaScript to detect this QS value (for example ?ph=768
), and if it's not there, redirect the page to itself, appending on ?ph=768
. That way you can use it to get the page height on document load.
As mentioned by @J.Steen in comments, though, it's probably better to define your intentions.
Upvotes: 3