用这个JavaScript在屏幕中心打开新窗口? [英] Open new window on center of screen with this javascript?

查看:96
本文介绍了用这个JavaScript在屏幕中心打开新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对javascript的使用经验非常有限。

 < script type =text / javascript我想要在屏幕中心打开新窗口。 > 
函数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>

如果有人可以更新我的代码来完成一个中心化的弹出窗口, / p>

解决方案

这是怎么回事(改编自这里):

 函数MyPopUpWin(url,width,height){
var leftPosition,topPosition;
//允许边框。
leftPosition =(window.screen.width / 2) - ((width / 2)+ 10);
//允许标题和状态栏。
topPosition =(window.screen.height / 2) - ((height / 2)+ 50);
//打开窗口。
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, =无);
}

通过替换 window.open(twtLink, ','width = 300,height = 300'); 在代码中使用 MyPopUpWin(twtLink,300,300);


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!

解决方案

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

这篇关于用这个JavaScript在屏幕中心打开新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆