簡體   English   中英

如何從源頭關閉瀏覽器選項卡

[英]How to close a browser tab from its origin

我可以打開一個新選項卡,然后從其父級中關閉它,如下所示:

<a id="mylink" href="javascript:" onclick="mynewwindow = window.open('https://www.google.co.in');" target="_blank">Sample Code</a>
    <a href="javascript:" onclick="closemywindow();">close</a>
    <script>
        var mynewwindow;
        function closemywindow(){
            mynewwindow.close();
        }
</script>

但是,如果通過上下文菜單或使用滾動條打開了新選項卡,如何關閉該新選項卡?

window.open的結果只是對窗口的另一個引用。 在您的頁面中,您可以只使用window.close。

請注意,如果這是唯一打開的選項卡,則可能會要求用戶允許關閉窗口。

感謝Darren的評論,我了解您可能想從某個中心關閉窗口。 可以使用外部Web服務。 您的鏈接可以采用http:// anotherpage?openedfrom = thispage的形式 ,然后新頁面可能會定期連接到中心點,並查看是否需要關閉。 在原始頁面上,您可能會指示服務“關閉由此打開的所有窗口”。 在這種情況下,“ thispage”將是隨機生成的用於標識打開頁面的密鑰。

如果您只想在當前瀏覽器上執行此操作,則甚至可能不需要使用Web服務,而是使用本地存儲

這是一些代碼(測試html文件稱為test13.html-適當更改名稱):

<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
<a href="test13.html" target="_blank">A link</a><br/>
<input type="button" value="Close all opened windows"/>
<script>
function guid() { //generates a random GUID like string
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });
}

window.windowKey=guid(); //generates a key for the window
//uncomment this if you want all the opened pages to share a key
//var m=/[\?&]windowKey=([^\?&]+)/.exec(document.location.href);
//if (m) window.windowKey=m[1];


if (!window.localStorage) {
    alert('No local storage, no autoclosing pages');
} else {
    $('input').click(function() { //click on the button to close all pages referred by this one
        window.localStorage.setItem(windowKey,"yes");
    });

    window.localStorage.setItem(windowKey,"no"); // you could skip this if you don't want to list the opened window keys
    $(window).on('beforeunload',function() {
        window.localStorage.removeItem(windowKey); // clean up localStorage
    });


    setInterval(function() {
        var m=/[\?&]windowKey=([^\?&]+)/.exec(document.location.href);
        if (!m) return;
        var referralKey=m[1]; //get the key from the windowKey URL parameter
        var shouldClose=window.localStorage.getItem(referralKey)=="yes";
        if (shouldClose) window.close(); //close this page if the key is marked for closing
    },1000);


    // you might want to execute this on window.ready
    $('a').each(function() { //add the window key to all the links on the page
        var a=$(this);
        var url=a.attr('href');
        url=url+(/\?/.test(url)?'&':'?')+'windowKey='+windowKey;
        a.attr('href',url);
    });
}
</script>

如果要支持這種情況:A頁打開B頁,然后打開C頁,然后A頁關閉B和C頁,請按如下所示更改導航線:

window.windowKey=guid();
var m=/[\?&]windowKey=([^\?&]+)/.exec(document.location.href);
if (m) window.windowKey=m[1];

一個解決方案可能是,在您所有頁面上都有一個腳本,該腳本帶有一個計時器,該計時器會關閉窗口,除非頁面上有鼠標事件。

document.addEventListener("DOMContentLoaded", function(event) { 
   var timeoutID;
   timeoutID = window.setTimeout(closeWindow, 2000);
   document.addEventListener('mousemove', clearAlert, false);

   function clearAlert() {
      window.clearTimeout(timeoutID);
      document.removeEventListener('mousemove', clearAlert, false);
   }
   function closeWindow() {
      window.close();
   }
});

我不確定它是否會正常工作,但它也可能總是會關閉移動設備上的窗口,否則瀏覽器將不會執行腳本,直到您切換到標簽頁

您無法關閉不是使用window.open()創建的窗口。 這將是一個非常糟糕的行為,例如,因為這樣做會同時刪除選項卡歷史記錄。 該選項卡屬於用戶,而不屬於您。

https://developer.mozilla.org/zh-CN/docs/Web/API/Window/close

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM