user1736794
user1736794

Reputation: 265

Open new window on center of screen with this javascript?

My experience with javascript is extraordinarily limited. I would like to have the new window open up in the center of the screen.

<script type="text/javascript">
function fbs_click() {
var twtTitle = document.title;
var twtUrl = location.href;
var maxLength = 140 - (twtUrl.length + 1);
if (twtTitle.length > maxLength) {
    twtTitle = twtTitle.substr(0, (maxLength - 3)) + '...';
}
var twtLink = 'http://twitter.com/home?status=' + encodeURIComponent(twtTitle + ' ' + twtUrl);
window.open(twtLink,'','width=300,height=300'); } </script>

If somebody can please update my code to accomplish a popup window which is centered that would be amazing!

Upvotes: 5

Views: 44176

Answers (1)

nick_w
nick_w

Reputation: 14938

How's this (adapted from here):

function MyPopUpWin(url, width, height) {
    var leftPosition, topPosition;
    //Allow for borders.
    leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
    //Allow for title and status bars.
    topPosition = (window.screen.height / 2) - ((height / 2) + 50);
    //Open the window.
    window.open(url, "Window2",
    "status=no,height=" + height + ",width=" + width + ",resizable=yes,left="
    + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY="
    + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
}

Call it by replacing window.open(twtLink,'','width=300,height=300'); in your code with MyPopUpWin(twtLink, 300, 300);

Upvotes: 21

Related Questions