Tuesday, April 13, 2010

Javascript OnLoad Page Resize and Move to Center

Here's a snippet for loading a web page with set width and height and moving it at the center of the screen.

window.onload = function() {
    var width = 800;
    var height = 600;
    window.resizeTo(width, height);
    window.moveTo(((screen.width - width) / 2), ((screen.height - height) / 2));

For this sample we resize the window to 800 by 600 and then placed it at the center of the screen by taking the screen width and height (current monitor resolution) then subtracting it with the set width and height (800 and 600 respectively) and finally diving it by 2. Now, simply place this script on the head part of the page and that's it.

No comments:

Post a Comment