9

Is it possible to refresh a page from another page using Javascript or JQuery without opening the same page in a new tab.

JS:

 var newtab = window.open('http://localhost:8081/app/home');
 newtab.document.location.reload(true);

I tried the above, but here, it will open a new tab, with the same page, which is already opened in the browser.

Please suggest a method.

Nayana_Das
  • 1,789
  • 4
  • 23
  • 48
  • Why would you want to refresh an external page? – DrRoach Nov 21 '14 at 12:32
  • I'm running two web applications, from one web app, am trying to refresh a page from second web app – Nayana_Das Nov 21 '14 at 13:03
  • @DrRoach, because those "external" pages may be *internal*, actually, to the same application, like multiple views of the same data set, or windows of concurrent user workflows etc. – Sz. Mar 04 '18 at 14:34

2 Answers2

6

I got the idea from a previous Question , here they used window Object Reference to reload the popup window, but for me it wont work, because, the parent window and child window runs in 2 different ports. So using the same trick, what i did is :

HTML:

<a onclick="openNewTab()">app2</a>

<a onclick="refreshExistingTab()">Refresh</a>

JS:

<script>

    var childWindow = "";
    var newTabUrl="http://localhost:8081/app/home";

    function openNewTab(){
        childWindow = window.open(newTabUrl);
    }

    function refreshExistingTab(){
        childWindow.location.href=newTabUrl;
    }

</script>

refreshExistingTab() this instend of refreshExistingTab

Mayur Vaghasiya
  • 1,383
  • 2
  • 12
  • 24
Nayana_Das
  • 1,789
  • 4
  • 23
  • 48
  • 1
    This is a great approach. One of the hidden benefits is that you can browse to other pages in that tab and upon clicking a new anchor it will still refresh that same tab :) – rich Mar 28 '17 at 00:27
3

take a look at https://developer.mozilla.org/en-US/docs/Web/API/Window.open basically if you do window.open and specify a window name it will overwrite that window with the url you provided. so if you open the page each time with same window name, it should overwrite it each time you do it again from that other page.

sagie
  • 1,744
  • 14
  • 15
  • i refered these two links http://stackoverflow.com/questions/16853138/send-a-refresh-request-to-another-page-opened-in-the-browser http://www.dynamicdrive.com/forums/showthread.php?42003-Refresh-one-page-from-another – Nayana_Das Nov 21 '14 at 12:40
  • can you explain a bit more because from these 2 links i get different requirements. 1 talks about 1 page open another. 2 talks about 2 pages which do not have parent/child relationship. your original question hinted that page 1 opened page 2. which one is it? if page 1 opens page 2, than page 1 can refresh page 2 by doing what i wrote initial with named windows, or via javascript like the first link you sent. however named window is stronger since you don't have to keep a reference to the window object in page 1 as you might have reload page 1 due to some scenario. – sagie Nov 21 '14 at 12:45
  • Sorry for the confusion, What i really want is explained in the question itself, i.e., i want to refresh a page which is already opened in the browser, from another page. Is it possible? – Nayana_Das Nov 21 '14 at 12:57
  • if you didn't open it or don't have a reference to it, than no. – sagie Nov 21 '14 at 12:59
  • What u mean by reference? can u explain? – Nayana_Das Nov 21 '14 at 13:00
  • 1
    like in your example newtab = window.open.... than newtab is the reference to the window. if you didn't open the window than hacking to other windows is a security issue and not possible. if the server is yours for both pages, you might be able to ping from both pages to the server to see if a refresh is needed but i don't have the full story here to understand. – sagie Nov 21 '14 at 13:02
  • I'm running two web applications, from one web app, am trying to refresh a page from second web app. – Nayana_Das Nov 21 '14 at 13:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65407/discussion-between-nayana-das-and-sagie-gur-ari). – Nayana_Das Nov 22 '14 at 04:34
  • @ sagie gur-ari :An upvote for u, for giving that link, from that i got the clue. Thanks u so much. – Nayana_Das Nov 24 '14 at 06:23