Unsafe attempt to initiate navigation for frame with origin when using window.close()

1.1k Views Asked by At

This a continuation of question asked here Refresh same tabs in javascript/react instead of opening new tab on browser window but a response was not received so i tried to follow different approaches myself.

I decided to go with the approach of saving the window references and closing the tabs using the same. What works here is: When i am able to open the new tabs immediately in browser for the same task. I save it in an array. I am able to close them immediately for same task: CODE BLOCK A:

               const openInNewTab = (url) => {
                        const newWindow = window.open(url, '_blank');  
                        prevWindow.push(newWindow);
                        if (newWindow) newWindow.opener = null
                   }
...
...
...
               popUpTabUrlArray.forEach((url,ind) => {
                    if(url && common.isValidHttpUrl(url))
                        openInNewTab(url);
                 });
               console.log("PREV WINDOW IS:", prevWindow); <<<<<< line 2
               if(prevWindow.length>0)
               {
                    prevWindow.forEach((win,ind)=>{
                        win.close();
                     });
                     prevWindow.current = [];
               }


In the above, when a task is fetched on my tool, the 3 new tabs open but they get closed immediately because i added the close function just below the open. It was added to check the close functionality.

However, i want it to close when i fetch new task. So when a new task is fetched:

  1. Old tabs should close
  2. New tabs should open up The component gets re-rendered when user saves current task and fetches new task. So this is what i did using useref: CODE BLOCK B:
let prevWindow = useRef([]);

const openInNewTab = (url) => {
            const newWindow = window.open(url, '_blank');  
            prevWindow.current.push(newWindow);
            if (newWindow) newWindow.opener = null
          }

...
...
return(
console.log("Previous window before opening new tab is:", prevWindow.current); <<<<<< line 3
if(prevWindow.current.length>0)
{
     prevWindow.current.forEach((win,ind)=>{ <<<<<<<<<<<<<<< line 1
           win.close();
      });
      prevWindow.current = [];
}
popUpTabUrlArray.forEach((url,ind) => {
      if(url && common.isValidHttpUrl(url))
           openInNewTab(url);
      })

What i want to achieve with above code is:

  1. First time prevWIndow is []
  2. When first task is fetched, prevWindow array will be empty. So no old tabs should be closed. While opening the new tab from popUpTabUrlArray - i push the window references in prevWindow which should be saved for next task.
  3. When second task is fetched, prevWindow array will have previous task window references- so it should first close those tabs and initialize the array to []. Now once they are close, the openInNewTab will be called for the new urls of second task and the second task new tab window references should be saved in prevWindow array.

What i am seeing here with above code execution is: The new tabs open as expected but when the next task is fetched and line 1 is executed for win.close(). This error is thrown:

Unsafe attempt to initiate navigation for frame with origin 'https://www.amazon.co.jp' from frame with URL 'http://localhost:3000/abc'. The frame attempting navigation is neither same-origin with the target, nor is it the target's parent or opener.

Can someone help me with this or suggest an alternative? I tried multiple approaches.

Additional

  1. I tried to print the prevWindow array in code block A wherein the close tabs for same task closes immediately(line 2). Here the array is like this where we can see it has Window references:
PREV WINDOW IS: (3) [Window, Window, Window]
  1. However, when i print the prevWindow array in code block B(line 3) where old tabs don't close and the error is thrown on console. The array is like this with some global references:
Previous window before opening new tab is: (3) [global, global, global]

DOes this global instead of window references has anything to do with the error?

0

There are 0 best solutions below