2014-10-30 196 views
-1

我打開彈出窗口使用Java腳本在當前活動屏幕後面。 這是我的簡單代碼。JavaScript彈出窗口(背景窗口)

var win = window.open('http://yahoo.com',null,"height=400,width=600,status=yes,toolbar=no,scrollbars=yes,menubar=yes,location=yes"); 
win.blur(); 
this.window.focus(); 

但它似乎並不適用於任何瀏覽器。它只是打開一個彈出窗口。我從stackoverflow中提到this question。但它不起作用。我的目標是在所有瀏覽器中創建一個背景窗口。有人請幫助我

+0

您正在測試這個瀏覽器? – Quince 2014-10-30 11:57:16

+0

你真的試圖打開雅虎還是你的孩子窗口? – artm 2014-10-30 11:58:35

+0

所有瀏覽器 – 2014-10-30 11:59:44

回答

1

你不能這樣做。現代Web瀏覽器不允許網站從其他選項卡中竊取焦點(因爲這對用戶來說可能會變得相當混亂)。唯一可以做的是提示用戶在新標籤中打開(這不會改變焦點)。或者你可以濫用警報找回焦點到您的網頁:

var win = window.open('http://yahoo.com',null,"target=_blank,height=400,width=600,status=yes,toolbar=no,scrollbars=yes,menubar=yes,location=yes"); 
alert("Welcome back"); 
+0

我該如何實現這一目標。我需要創建背景窗口 – 2014-10-30 12:05:09

+0

@ ling.s alert()應該把重點放在主窗口但afaik,而不是跨瀏覽器 – 2014-10-30 12:09:36

+0

沒有警報? – 2014-10-30 12:10:20

-1
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/」);