Javascript 在 chrome 下弹出一个窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4261784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a window pop under in chrome
提问by prgrmr
I have a button that needs to open a new window as a popup (under the parent page). In IE/Firefox, it works fine, but in chrome the popup appears over (on top of) the parent window.
我有一个按钮需要打开一个新窗口作为弹出窗口(在父页面下)。在 IE/Firefox 中,它工作正常,但在 chrome 中,弹出窗口出现在父窗口上方(顶部)。
Please suggest a fix.
请建议修复。
use case/eg: Well, for eg if you see kayak.com or any travel website, you have the ability to search on other websites too..I want to do something similar so need the pop under...
用例/例如:嗯,例如,如果您看到 kayak.com 或任何旅游网站,您也可以在其他网站上进行搜索..我想做类似的事情,所以需要弹出...
Code: I am using a window.open(.......).blur(), but for some reason it isnt working in chrome.
代码:我正在使用 window.open(.......).blur(),但由于某种原因它在 chrome 中不起作用。
回答by Jason Benson
I take back my comment, is possible.
我收回我的评论,这是可能的。
The following worked for me. (tested latest production chrome)
以下对我有用。(测试最新生产的铬)
var url = "yourURL.html";
window.open(url, "s", "width= 640, height= 480, left=0, top=0, resizable=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no").blur();
window.focus();
Like all things, if you annoy your visitors you will have less visitors.
像所有事情一样,如果您惹恼了访问者,那么访问者就会减少。
回答by dixie
function makePopunder(pUrl) {
var _parent = (top != self && typeof (top["document"]["location"].toString()) === "string") ? top : self;
var mypopunder = null;
var pName = (Math["floor"]((Math["random"]() * 1000) + 1));
var pWidth = window["innerWidth"];
var pHeight = window["innerHeight"];
var pPosX = window["screenX"];
var pPosY = window["screenY"];
var pWait = 3600;
pWait = (pWait * 1000);
var pCap = 50000;
var todayPops = 0;
var cookie = "_.mypopunder";
var browser = function () {
var n = navigator["userAgent"]["toLowerCase"]();
var b = {
webkit: /webkit/ ["test"](n),
mozilla: (/mozilla/ ["test"](n)) && (!/(compatible|webkit)/ ["test"](n)),
chrome: /chrome/ ["test"](n),
msie: (/msie/ ["test"](n)) && (!/opera/ ["test"](n)),
firefox: /firefox/ ["test"](n),
safari: (/safari/ ["test"](n) && !(/chrome/ ["test"](n))),
opera: /opera/ ["test"](n)
};
b["version"] = (b["safari"]) ? (n["match"](/.+(?:ri)[\/: ]([\d.]+)/) || [])[1] : (n["match"](/.+(?:ox|me|ra|ie)[\/: ]([\d.]+)/) || [])[1];
return b;
}();
function isCapped() {
try {
todayPops = Math["floor"](document["cookie"]["split"](cookie + "Cap=")[1]["split"](";")[0]);
} catch (err) {};
return (pCap <= todayPops || document["cookie"]["indexOf"](cookie + "=") !== -1);
};
function doPopunder(pUrl, pName, pWidth, pHeight, pPosX, pPosY) {
if (isCapped()) {
return;
};
var sOptions = "toolbar=no,scrollbars=yes,location=yes,statusbar=yes,menubar=no,resizable=1,width=" + pWidth.toString() + ",height=" + pHeight.toString() + ",screenX=" + pPosX + ",screenY=" + pPosY;
document["onclick"] = function (e) {
if (isCapped() || window["pop_clicked"] == 1 || pop_isRightButtonClicked(e)) {
//return;
};
window["pop_clicked"] = 1;
mypopunder = _parent["window"]["open"](pUrl, pName, sOptions);
if (mypopunder) {
var now = new Date();
document["cookie"] = cookie + "=1;expires=" + new Date(now["setTime"](now["getTime"]() + pWait))["toGMTString"]() + ";path=/";
now = new Date();
document["cookie"] = cookie + "Cap=" + (todayPops + 1) + ";expires=" + new Date(now["setTime"](now["getTime"]() + (84600 * 1000)))["toGMTString"]() + ";path=/";
pop2under();
};
};
};
function pop2under() {
try {
mypopunder["blur"]();
mypopunder["opener"]["window"]["focus"]();
window["self"]["window"]["blur"]();
window["focus"]();
if (browser["firefox"]) {
openCloseWindow();
};
if (browser["webkit"]) {
openCloseTab();
};
} catch (e) {};
};
function openCloseWindow() {
var ghost = window["open"]("about:blank");
ghost["focus"]();
ghost["close"]();
};
function openCloseTab() {
var ghost = document["createElement"]("a");
ghost["href"] = "about:blank";
ghost["target"] = "PopHelper";
document["getElementsByTagName"]("body")[0]["appendChild"](ghost);
ghost["parentNode"]["removeChild"](ghost);
var clk = document["createEvent"]("MouseEvents");
clk["initMouseEvent"]("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
ghost["dispatchEvent"](clk);
window["open"]("about:blank", "PopHelper")["close"]();
};
function pop_isRightButtonClicked(e) {
var rightclick = false;
e = e || window["event"];
if (e["which"]) {
rightclick = (e["which"] == 3);
} else {
if (e["button"]) {
rightclick = (e["button"] == 2);
};
};
return rightclick;
};
if (isCapped()) {
return;
} else {
doPopunder(pUrl, pName, pWidth, pHeight, pPosX, pPosY);
};
}
makePopunder("http://www.yourdomain.com/");
回答by A. Wolff
This is the fix you can use for Chrome (tested on lastest v.40 on 29/01/2015).
This won't open a window popup but a new tab and keeps on main tab focused(no more keeps focus on main tab on chrome v.43>).
这是您可以用于 Chrome 的修复程序(在 2015 年 1 月 29 日在最新的 v.40 上测试)。
这不会打开一个窗口弹出窗口,而是一个新选项卡,并保持主选项卡的焦点(不再专注于 chrome v.43> 的主选项卡)。
To avoid popup blocker, you need user interaction, use specifically mousedown
or mouseup
event, click
will throw a popup blocker warning.
为避免弹出窗口拦截器,需要用户交互,使用专门mousedown
或mouseup
事件,click
会抛出弹出窗口拦截器警告。
document.addEventListener("mousedown", tabUnder);
function tabUnder() {
var a = document.createElement("a"),
e = document.createEvent("MouseEvents");
a.href = "http://testit.com"; //the URL of 'popup' tab
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
a.dispatchEvent(e);
document.removeEventListener("mousedown", tabUnder);
}
-jsFiddle-
-jsFiddle-
回答by Diogo Schneider
You could also leave the popup behind, like this:
您也可以将弹出窗口留在后面,如下所示:
var MINUTE_MILLISECONDS = 60000;
var now = new Date().getTime();
if (!localStorage.t || now > parseInt(localStorage.t) + MINUTE_MILLISECONDS) {
var date = new Date();
localStorage.t = now;
window.location.href = "http://dgsprb.blogspot.com/";
window.open(window.document.URL, "_blank");
}
This way the new content is left behind on the current tab, opening a new tab with the original window content. Works pretty much like a pop under, provided you can afford to reload the current window. You also ensure that the popup won't be shown more than once per minute.
这样,新内容会留在当前选项卡上,打开一个带有原始窗口内容的新选项卡。如果您有能力重新加载当前窗口,它的工作方式非常类似于弹出窗口。您还要确保弹出窗口每分钟不会显示超过一次。
回答by Bonswouar
The misterious code of @dixie works for me on firefox, I.E, and almost Chrome (it doesn't focus on the main window but on the pop-up).
@dixie 的神秘代码在 Firefox、IE 和几乎 Chrome 上对我有用(它不关注主窗口,而是关注弹出窗口)。
To make it perfectly work on Google Chrome, I simply added this to regain focus:
为了让它在谷歌浏览器上完美运行,我简单地添加了这个以重新获得焦点:
path = window.document.URL;
window.open(path,"_self");
回答by Chandler_bing_26
The end of popunder is here. Chrome closed it yesterday.
popunder 到此结束。Chrome 昨天关闭了它。
回答by Pitu
This code works up to Chrome 65:
此代码适用于 Chrome 65:
function just_open() {
postMessage([...arguments]);
}
window.onmessage = function({data}){
return open(...data);
}
function openunder() {
just_open([...arguments]);
window.open().close();
}
Where openunder is just like open() except:
openunder 就像 open() 一样,除了:
- no return value
- No Chrome 65+, uses https://crbug.com/833148
- 没有返回值
- 没有 Chrome 65+,使用https://crbug.com/833148
回答by aljgom
window.open('http://google.com','','height=500,width=500');
window.open().close();
Don't use popunders for evil
不要将popunders用于邪恶