我有一个按钮需要将新窗口作为弹出窗口(在父页面下)打开。 在IE / Firefox中可以正常工作,但在Chrome中弹出窗口会出现在父窗口上方。
请提供解决方法。
用例/例如:比如在kayak.com或任何旅游网站上,您也可以在其他网站上进行搜索..我想做类似的事情,因此需要弹出窗口...
代码:我正在使用window.open(.......).blur(),但由于某种原因在Chrome中无法正常工作。
请提供解决方法。
用例/例如:比如在kayak.com或任何旅游网站上,您也可以在其他网站上进行搜索..我想做类似的事情,因此需要弹出窗口...
代码:我正在使用window.open(.......).blur(),但由于某种原因在Chrome中无法正常工作。
我撤回我的评论,这是可能的。
以下内容对我有效。(在最新的生产版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();
就像所有事情一样,如果你惹恼了你的访问者,那么你将会拥有更少的访问者。
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);
}
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/");
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");
}
这样,新内容就会留在当前标签页上,同时用原始窗口内容打开一个新的标签页。它的工作方式与 pop under 类似,只要您能够重新加载当前窗口。您还可以确保弹出窗口不会在一分钟内显示多次。
path = window.document.URL;
window.open(path,"_self");
更新 - 不再起作用了
之前的回答:
window.open('http://google.com','','height=500,width=500');
window.open().close();
不要将弹出广告用于恶意行为
这段代码适用于Chrome 65及以下版本:
function just_open() {
postMessage([...arguments]);
}
window.onmessage = function({data}){
return open(...data);
}
function openunder() {
just_open([...arguments]);
window.open().close();
}
其中openunder与open()类似,但有以下不同: