窗口打开后调整大小以适应可用宽度和高度

6

我稍微搜索了一下,但是并没有找到这是否可行。我想使用window.open()方法打开链接到窗口的可用宽度和高度。类似于以下代码。

var h = $(window).height(); 
var w = $(window).width(); 

$('#window-opener').live('click',function (e) {
        window.open(this.href, 'Resource', 'toolbar=no ,location=0, status=no, titlebar=no, menubar=no,
                    width='+w', 
                    height='+h);
        e.preventDefault();
});

这可行吗?如果不行,有没有人能推荐类似的方法。

你试过了吗?把你的变量声明移到函数内,然后你就可以运行了。 - Jay Blanchard
2个回答

8

你的代码是正确的,只是在宽度拼接后面缺少了一个单引号:

width='+w', 

必须是

width='+ w +', 

我已经尝试过这个了,也许我不太理解你真正想做什么:
var h = screen.height;
var w = screen.width;

$('#window-opener').live('click',function (e) {
    window.open(this.href, 'Resource', 'toolbar=no ,location=0, 
    status=no,titlebar=no,menubar=no,width='+w +',height=' +h);
    e.preventDefault();
});​

Fiddle: http://jsfiddle.net/UC8Ww/


1
上面的h和w变量的赋值混淆了。应该写成var h = screen.height,而不是var h = screen.width,宽度变量也是一样。 - Robini

2

因为您构建字符串的方式不正确。

width='+w',height='+h);

你看到你错过的了吗?希望你看到了缺少的+


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接