javascript - 使用此 javascript 在屏幕中央打开新窗口?

标签 javascript html popup popupwindow

我在 javascript 方面的经验非常有限。我想让新窗口在屏幕中央打开。

<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>

如果有人可以更新我的代码来实现一个居中的弹出窗口,那就太棒了!

最佳答案

怎么样(改编自 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");
}

通过将代码中的 window.open(twtLink,'','width=300,height=300'); 替换为 MyPopUpWin(twtLink, 300, 300); 来调用它

关于javascript - 使用此 javascript 在屏幕中央打开新窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12869946/

相关文章:

html - 将背景图像设置为具有部分透明度的图像

html - Scala 字符串集合——一种输出不带双引号的字符串的方法

javascript - 是否有子窗口打开时触发的事件?

javascript - js onClick="window.open 父加载基础 url

java - 按 "ctrl"+ "alt"+ "backspace"后执行某项任务

javascript - 在 web2py 中更新图像

javascript - jQuery 切换修改

Javascript从打开的窗口获取元素

javascript - 如何使用javascript清除 Canvas 层并将其另存为图像

javascript - 使 <a> 链接不执行默认功能