Open new window on center of screen with this javascript?(使用此 javascript 在屏幕中心打开新窗口?)
问题描述
我的 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!
推荐答案
怎么样(改编自这里):
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); 来调用它
Call it by replacing window.open(twtLink,'','width=300,height=300');
in your code with MyPopUpWin(twtLink, 300, 300);
这篇关于使用此 javascript 在屏幕中心打开新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用此 javascript 在屏幕中心打开新窗口?


- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01