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

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

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

I have an eventAddListener that loads http://google.com on a new tab with a button press, but right after it opens the new tab of google.com, I want it to REFRESH again. NOT my base page but the NEW tab page, by itself. The code I showed is just one of the examples out of 5 pages worth of google search which don't work.

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

This is the best i could come up with. It opens new tab and "Reloads" the new tab rather than refresh it.

What I want is for example, you click on new tab, you paste a link then press enter, which EXECUTES the link. I basically want a javascript function which EXECUTES the link.

  • `childWindow` is a string, which does not have a `location` property. Set it equal to your `window.open` statement. – Heretic Monkey Jul 24 '21 at 17:08
  • Does this answer your question? [refresh child window from parent window](https://stackoverflow.com/questions/4059179/refresh-child-window-from-parent-window) – Heretic Monkey Jul 24 '21 at 17:10
  • It appears you just copied the code from the answer incorrectly. – Heretic Monkey Jul 24 '21 at 17:11
  • Still doesn't work unfortunately. Closest i ever got was using this code var win = window.open('http://google.com', 'New Window'); setTimeout(function () { var win = window.open('http://google.com', 'New Window'); },3000); – drake leubin Jul 24 '21 at 17:47
  • @drakeleubin you just need `win.location.reload()`, and then it would work properly – Jake Jul 24 '21 at 20:54
  • “Doesn’t work” is not sufficient to say that this is not the same question. [Edit] your question with the exact code you tried and what errors show up. Also, some indication of how you are testing whether the code works if no errors are present. – Heretic Monkey Jul 24 '21 at 21:14
  • To be honest i don't know how to edit the main post. What i want is like for example, you click on new tab, you paste a link, and then you press ENTER, which executes the link. That is what i want. Not just open the link in new tab. I want javascript to "Execute" the link – drake leubin Jul 25 '21 at 09:36

3 Answers3

0

From my understanding you want the new tab to refresh once opened with JavaScript instead of the current tab where you run the JavaScript code from.

That's not directly possible. The JavaScript code will only run for the tab it was executed in. The newly opened tab does not know that JavaScript code should be running. The current tab cannot pass over instructions for the new tab to execute.

However, you can select the newly opened tab manually first and then execute Javascript code to refresh the page. But that probably defeats the purpose of what you're trying to do.

Oushima
  • 250
  • 3
  • 7
  • Probably true. Closest i ever got is using this var win = window.open('http://google.com', 'New Window'); setTimeout(function () { var win = window.open('http://google.com', 'New Window'); },3000); – drake leubin Jul 24 '21 at 17:48
0

You can't do this.

In order to trigger a reload in the new tab/window you need to run JS in that tab/window.

The same origin policy prevents this.

If you had control over the new page then you could have an event listener running in it and post a message asking that listener to trigger a refresh.

Obviously you can't do that with Google's page.


This question, however, reads like an XY problem. If the goal is to display fresh (and not old, cached, out of date) information and the target page is not a third party one then you shouldn't be using JS to hack your way around caching. Instead set better caching rules on the target page in the first place.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Probably true. Closest i ever got was using this line of code. var win = window.open('http://google.com', 'New Window'); setTimeout(function () { var win = window.open('http://google.com', 'New Window'); },3000); – drake leubin Jul 24 '21 at 17:49
  • @drakeleubin Please don’t copy an paste the exact same text across multiple comments. If you want to share information to everyone, edit your question with the info. – Heretic Monkey Jul 24 '21 at 21:08
  • Sorry, i tried editing my post yesterday but a weird, visual studio esque comparison came up instead of a simple edit input. Fixed it now. – drake leubin Jul 25 '21 at 09:40
0

I worked on something similar in the past few weeks and the code below worked for me.

index.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>

Just to prove that this works on the new tab I used the code

<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>

Hopefully that's what you are looking for.

Relcode
  • 505
  • 1
  • 6
  • 16
  • Thanks for the help but doesn't work for me unfortunately. Closest i ever got is this var win = window.open('http://google.com', 'New Window'); setTimeout(function () { var win = window.open('http://google.com', 'New Window'); },3000); – drake leubin Jul 24 '21 at 17:47
  • No problem... I'm really curious to know what exactly you wanted to do so when you have found a solution can you please let me see it? All the best on your project. – Relcode Jul 27 '21 at 10:41