建立一个链接打开一个新窗口(而不是标签)有没有办法让链接打开一个新的浏览器窗口(而不是选项卡)而不使用JavaScript?
3 回答
慕的地8271018
TA贡献1796条经验 获得超4个赞
这将打开一个新窗口,而不是tab(使用JavaScript,但非常简洁):
<a href="print.html"
onclick="window.open('print.html',
'newwindow',
'width=300,height=250');
return false;"
>Print</a>
12345678_0001
TA贡献1802条经验 获得超5个赞
我知道它有点旧Q但是如果你通过搜索解决方案来到这里,所以我通过jquery得到了一个很好的
jQuery('a[target^="_new"]').click(function() {
var width = window.innerWidth * 0.66 ;
// define the height in
var height = width * window.innerHeight / window.innerWidth ;
// Ratio the hight to the width as the user screen ratio
window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));});它将<a target="_new">在一个新窗口中打开所有内容
编辑:
1,我在原始代码中做了一些小改动现在它完全按照用户屏幕比例打开新窗口(对于横向桌面)
但是,我建议您使用以下代码,如果您在移动设备中打开新标签中的链接:
jQuery('a[target^="_new"]').click(function() {
return openWindow(this.href);}function openWindow(url) {
if (window.innerWidth <= 640) {
// if width is smaller then 640px, create a temporary a elm that will open the link in new tab
var a = document.createElement('a');
a.setAttribute("href", url);
a.setAttribute("target", "_blank");
var dispatch = document.createEvent("HTMLEvents");
dispatch.initEvent("click", true, true);
a.dispatchEvent(dispatch);
}
else {
var width = window.innerWidth * 0.66 ;
// define the height in
var height = width * window.innerHeight / window.innerWidth ;
// Ratio the hight to the width as the user screen ratio
window.open(url , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
}
return false;}- 3 回答
- 0 关注
- 640 浏览
相关问题推荐
添加回答
举报
0/150
提交
取消
