- 1). Launch your HTML editor and open a Web page document.
- 2). Add the following "<script>" section after your document's opening "<head>" tag:
<script type="text/javascript">
</script> - 3). Paste the following JavaScript code after the opening "<script>" tag:
<script type="text/javascript">
window.onload=function() {
getPixels();
}
function getPixels() {
var pixelWidth = screen.width;
var pixelHeight = screen.height;
var pageSize = "Page Width = " + pixelWidth + " .. Page Height = " + pixelHeight;
alert(pageSize);
return pageSize;
}
</script>
The "window.unload" function calls a function named "getPixels" when a browser loads the page. The "getPixels" function, shown after the "window.onload" function, retrieves the page's dimensions by checking the "screen.width" and "screen.height" properties. Those properties contains the dimensions of the user's entire screen. The function then displays a pop-up alert window showing you those dimensions. - 4). Save the HTML document and open it in a browser. The getPixels function runs and displays the page's size in pixels.
previous post