Click here to Skip to main content
15,887,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web page that opens the parent page and activates it, leaving the child open. I have used the following JavaScript for years to do this.
OnClientClick="window.opener.location.reload(true)" 

But it no longer works in Firefox. How do I fix it? It seems to still work in Chrome. I had a similar issued for which I got help here. JavaScript is similar except it closed the child page.

What I have tried:

I tried several suggestions that I found on the web. None worked.
Posted
Updated 24-Jun-23 1:18am

1 solution

Some browsers updated their security policies and your issue might be due to stricter security policies that were implemented to prevent this kind of direct interaction between windows that are opened from different origins.

To ensure cross-browser compatibility, I would use the 'window.postMessage()' method as this method exchange messages between windows, even across different origins.

In the parent page, I will add the following to use window.postMessage() -
//handle messages from the child page
function receiveMessageFromChild(event) {
  if (event.origin !== window.location.origin) {
    // Reject messages from unknown origins for security
    return;
  }

  // actions when message is received from the child page
  // for example, reload the parent page
  if (event.data === "reloadParent") {
    window.location.reload(true);
  }
}

// receive messages from the child page
window.addEventListener("message", receiveMessageFromChild, false);

// function to open the child page
function openChildPage() {
  // open the child page
  var childPage = window.open("child.html");

  // actions after opening the child page
  // for example, send a message to the child page
  childPage.postMessage("activateParent", window.location.origin);
}


and in the child page -
// handle messages received from the parent page
function receiveMessageFromParent(event) {
  if (event.origin !== window.location.origin) {
    // reject messages from unknown origins for security
    return;
  }

  // actions when message is received from the parent page
  // for example, notify the parent to reload
  if (event.data === "activateParent") {
    window.opener.postMessage("reloadParent", window.location.origin);
  }
}
// receive messages from the parent page
window.addEventListener("message", receiveMessageFromParent, false);


You can read more on this method at Window: postMessage() method[^]
 
Share this answer
 
Comments
BobbyStrain 24-Jun-23 12:23pm    
Thank you, Andre. I wonder how long my mature pages will last. I probably would not have found this solution by searching.
Andre Oosthuizen 25-Jun-23 5:45am    
It's a pleasure, I hope your pages last for some time. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900