第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

window.opener !== window 僅在 Mozilla 中定位為“_self”

window.opener !== window 僅在 Mozilla 中定位為“_self”

繁華開滿天機(jī) 2021-08-20 15:48:11
我使用 window.open 作為目標(biāo)“ _self ”從父窗口打開了一個(gè)子窗口,當(dāng)我嘗試console.log(window.opener !== window)在子窗口中打開它時(shí),它在Mozilla 中返回 false ,但工作正常并在chrome 中返回true。我想要的是原始父窗口實(shí)例的引用。<!DOCTYPE html><html>  <head>    <title>Parcel Sandbox</title>    <meta charset="UTF-8" />  </head>  <body>    <div id="app"></div>    <button onclick="myFunction()">Try it</button>    <script>      function myFunction() {        var wnd = window.open("about:blank", "_self");        wnd.document.write(          "blank\<script\>alert(window.opener !== window)\</script\>"        );        console.log(wnd);      }    </script>  </body></html>
查看完整描述

2 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

根據(jù)mdn


使用說(shuō)明

當(dāng)一個(gè)窗口從另一個(gè)窗口打開時(shí)(使用 Window.open 或具有其目標(biāo)屬性集的鏈接),它維護(hù)對(duì)第一個(gè)窗口的引用作為 window.opener。如果當(dāng)前窗口沒(méi)有開啟器,則此方法返回 null。


所以我希望window.open以自我為目標(biāo)null,因此


window.opener !== window // 真


無(wú)論是錯(cuò)誤還是有意 - FF68.0.1 和 Chrome75.0.3770.142 似乎沒(méi)有同意


作為一種解決方法模擬_opener?


<!DOCTYPE html>

<html>

  <head>

    <title>Parcel Sandbox</title>

    <meta charset="UTF-8" />

  </head>


  <body>

    <div id="app"></div>

    <button onclick="myFunction()">Try it</button>


    <script>

      function myFunction() {

        window._opener = window.open("about:blank", "_self");

       window._opener.document.write(

          "blank\<script\>alert(window._opener !== window)\</script\>"

        );

        

      }

    </script>

  </body>

</html>


查看完整回答
反對(duì) 回復(fù) 2021-08-20
?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

引用 MDN:


如果同名的窗口已經(jīng)存在,則strUrl加載到現(xiàn)有窗口中。在這種情況下,該方法的返回值是現(xiàn)有窗口并被strWindowFeatures忽略。提供空字符串strUrl是一種通過(guò)名稱獲取對(duì)打開窗口的引用而不更改窗口位置的方法。


所以當(dāng)你做


window.open(url, '_self');

您實(shí)際上只是將當(dāng)前的 window 設(shè)置為 this url,但 Window 對(duì)象不會(huì)更改。


btn.onclick = e => {

  const wnd = window.open('', '_self'); // let's not change the url

  console.log(window === wnd);

};

<button id="btn">click me</button>

所以你的代碼片段實(shí)際上只是更新你當(dāng)前的文檔:

function myFunction() {

  window.document.write(

    "blank\<script\>alert(window.opener !== window)\</script\>"

  );

}

<button onclick="myFunction()">Try it</button>

然而,奇怪的是 Firefox 確實(shí)將 更新openedWindow.opener為任何窗口將window.open()以其名稱調(diào)用。
這確實(shí)是一個(gè)錯(cuò)誤,并且是最終導(dǎo)致您出現(xiàn)問(wèn)題的原因,因?yàn)樗鼞?yīng)該保持原狀(null在您的示例代碼段中)。


但請(qǐng)注意,您嘗試執(zhí)行的核心操作(獲取“原始父窗口實(shí)例的引用”)在此處無(wú)關(guān)緊要,因?yàn)槟拇a片段中始終只有一個(gè) Window。


查看完整回答
反對(duì) 回復(fù) 2021-08-20
  • 2 回答
  • 0 關(guān)注
  • 244 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)