Javascript 用铬合金制作窗户
我有一个按钮,需要打开一个新窗口作为弹出窗口(在父页面下)。在IE/Firefox中,它可以正常工作,但在chrome中,弹出窗口会出现在父窗口的上方 请提出解决办法 用例/eg:例如,如果你看到kayak.com或任何旅游网站,你也有能力在其他网站上搜索..我想做一些类似的事情,所以需要在Javascript 用铬合金制作窗户,javascript,google-chrome,popup,Javascript,Google Chrome,Popup,我有一个按钮,需要打开一个新窗口作为弹出窗口(在父页面下)。在IE/Firefox中,它可以正常工作,但在chrome中,弹出窗口会出现在父窗口的上方 请提出解决办法 用例/eg:例如,如果你看到kayak.com或任何旅游网站,你也有能力在其他网站上搜索..我想做一些类似的事情,所以需要在 代码:我使用的是window.open(……).blur(),但由于某些原因,它在chrome中不起作用。我收回我的评论,这是可能的 以下几点对我有用。(测试最新生产的铬) 和所有事情一样,如果你惹恼了你的
代码:我使用的是window.open(……).blur(),但由于某些原因,它在chrome中不起作用。我收回我的评论,这是可能的 以下几点对我有用。(测试最新生产的铬) 和所有事情一样,如果你惹恼了你的访客,你的访客就会减少
函数makePopunder(pUrl){
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_parent=(top!=self&&typeof(top[“document”][“location”].toString())==“string”)?top:self;
var mypopunder=null;
var pName=(数学[“地板]((数学[“随机”]()*1000)+1));
var pWidth=窗口[“内部宽度”];
var pHeight=窗口[“内部高度”];
var pPosX=窗口[“屏幕X”];
var pPosY=窗口[“屏幕”];
var pWait=3600;
pWait=(pWait*1000);
var-pCap=50000;
var todayPops=0;
var cookie=“\uu.mypopunder”;
var browser=函数(){
var n=navigator[“userAgent”][“toLowerCase”]();
变量b={
webkit:/webkit/[“测试”](n),
mozilla:(/mozilla/[“测试”](n))&&(!/(兼容的webkit)/[“测试”](n)),
chrome:/chrome/[“测试”](n),
msie:(/msie/[“测试”](n))&&(!/opera/[“测试”](n)),
firefox:/firefox/[“测试”](n),
safari:(/safari/[“测试”](n)和&!(/chrome/[“测试”](n)),
opera:/opera/[“测试”](n)
};
b[“版本”]=(b[“狩猎”]?(n[“匹配”](/。+(?:ri)[\/:]([\d.]+)/)|[]):(n[“匹配”](/。+(?:ox | me | ra | ie)[\/:]([\d.]+)/)| | |[])[1];
返回b;
}();
函数isCapped(){
试一试{
todayPops=Math[“floor”](文档[“cookie”][“split”](cookie+“Cap=”)[1][“split”](“;”)[0]);
}捕获(错误){};
return(pCap对于我来说,@dixie的错误代码适用于firefox,也就是说,几乎适用于Chrome(它不关注主窗口,而是关注弹出窗口)
为了让它在Google Chrome上完美运行,我简单地添加了以下内容以重新获得关注:
path = window.document.URL;
window.open(path,"_self");
您也可以将弹出窗口留在后面,如下所示:
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");
}
通过这种方式,新内容将留在当前选项卡上,使用原始窗口内容打开一个新选项卡。如果您能够重新加载当前窗口,它的工作方式与弹出窗口非常相似。您还可以确保弹出窗口不会每分钟显示一次以上。这是您可以用于Chrome的修复程序(在2015年1月29日的最新v.40上测试)。
这不会打开一个弹出窗口,但会打开一个新选项卡并保持主选项卡的焦点(不再保持chrome v.43>上主选项卡的焦点)
要避免弹出窗口阻止程序,您需要用户交互,具体使用mousedown
或mouseup
事件,单击
将抛出弹出窗口阻止程序警告
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);
}
popunder的结尾在这里。
Chrome昨天关闭了它。更新-不再工作了
先前的答案:
window.open('http://google.com','','height=500,width=500');
window.open().close();
不要将Popender用于邪恶此代码适用于Chrome 65:
function just_open() {
postMessage([...arguments]);
}
window.onmessage = function({data}){
return open(...data);
}
function openunder() {
just_open([...arguments]);
window.open().close();
}
其中openunder与open()类似,除了:
- 无返回值
- 无铬65+,使用
@TheifMaster:我可以很容易地证明弹出窗口的合理性。而不是弹出窗口。@ideate:Kayak使用弹出窗口来实现此功能,而不是弹出窗口。我必须承认,我对回答“不要这样做,因为这对我来说很烦人”的答案或评论感到非常厌倦。类似于“如何在chrome中创建弹出窗口”的问题确实是有效的。如果你的道德准则阻止你回答,那么我很感激(也可能分享你的道德),但这里没有上尉道德徽章。(顺便说一句@ideate,简短的回答是你不能。长答案是,和所有长答案一样,在无限的时间和资源下,任何事情都是可能的。)/rant(反映了我的道德观,我对@T.J.s.的评论投了赞成票)@Pekka你可能是对的,但因为其他人都在他们的肥皂盒上,我决定把我的也拖出来。@Pekka:不幸的是,Chrome并没有击败kayak.com(或travelocity.com)的流行广告:-(@Jason:我同意,我已经在StackOverflow上发布了如何做弹出窗口,因为有很多有效的用例。也许我只是有点古怪,但在缺乏真正的用例的情况下,我只是不想帮助弹出窗口。也许我太过分了,建议其他人不应该这样做,这是公平的。:-)+1因为它回答了这个问题,但事实仍然是,没有人应该使用在Chrome中工作的pop-unders。有人知道在Firefox中也能工作的解决方案吗?我有一个工作了一段时间的解决方案,但似乎被最新的更新打破了。您的窗口功能中同时有resizable=yes和resizable=no;)下定决心我有一个网站,我希望用户能够提交表单并触发下载大文件。我将表单的操作设置为“\u blank”,下载前必须处理的数据量很大,因此创建了新选项卡并挂起大约20秒。我需要该选项卡以防出现错误页面,这样他们就可以继续在主窗口上执行其他工作。我认为此下载选项卡的弹出窗口将是弹出窗口的一个非常好的合法用途。但是,此代码是doesn似乎不再工作了(不管怎样,在Chrome上)。代码似乎是说项目被放弃了。看起来不错。还有如何在页面加载后自动打开popunder
function just_open() {
postMessage([...arguments]);
}
window.onmessage = function({data}){
return open(...data);
}
function openunder() {
just_open([...arguments]);
window.open().close();
}