簡體   English   中英

用於重新加載新標簽頁的 Javascript

[英]Javascript to reload a new tab page

window.open("http://google.com", '_blank');

var childWindow = "http://google.com";

childWindow.location.href = "http://google.com";

我有一個eventAddListener ,它通過按下按鈕在新選項卡上加載http://google.com ,但在它打開 google.com 的新選項卡后,我希望它再次刷新。 不是我的基頁,而是新標簽頁本身。 我展示的代碼只是 5 頁谷歌搜索中不起作用的示例之一。

更新:var win = window.open('google.com', 'New Window'); setTimeout(function () { var win = window.open('google.com', 'New Window'); },3000);

這是我能想到的最好的方法。 它打開新選項卡並“重新加載”新選項卡而不是刷新它。

例如,我想要的是,您單擊新選項卡,粘貼鏈接,然后按 Enter,執行鏈接。 我基本上想要一個執行鏈接的 javascript 函數。

根據我的理解,您希望新選項卡在使用 Javascript 打開后刷新,而不是從運行 Javascript 代碼的當前選項卡。

這不是直接可能的。 您只能從當前選項卡執行 Javascript 代碼。 新打開的選項卡不知道 Javascript 代碼應該運行。 當前選項卡不能傳遞新選項卡執行的指令。

但是,您可以先選擇新打開的選項卡,然后執行 Javascript 代碼來刷新頁面。 但這可能違背了你想要做的事情的目的。

你不能這樣做。

為了在新選項卡/窗口中觸發重新加載,您需要在該選項卡/窗口中運行 JS。

同源策略可以防止這種情況。

如果您可以控制新頁面,那么您可以在其中運行一個事件偵聽器並發布一條消息,要求該偵聽器觸發刷新。

顯然你不能用谷歌的頁面來做到這一點。


然而,這個問題讀起來像一個XY 問題 如果目標是顯示新鮮(而不是舊的、緩存的、過時的)信息並且目標頁面不是第三方頁面,那么您不應該使用 JS 來破解緩存。 而是首先在目標頁面上設置更好的緩存規則

在過去的幾周里,我做了一些類似的工作,下面的代碼對我有用。

索引.html

<button onclick="openTab()">New Tab</button>

<script>
    function openTab(){
        //this opens a new tab while creating a variable name for that tab
        var newTab = window.open("https://google.com","_blank");
        //this refreshes that new tab
        newTab.location.reload();
    }
</script>

只是為了證明這適用於我使用的代碼的新選項卡

<script>
    function openTab(){
        //this opens a new tab while creating a variable name for that tab
        var newTab = window.open("https://google.com","_blank");
        //this will alert in the new tab
        newTab.alert("New Tab");
        //before the following reload code takes effect
        //this refreshes that new tab
        newTab.location.reload();
    }
</script>

希望這就是你正在尋找的。

暫無
暫無

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

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