Issue accessing parent URL from child window - javascript

Suppose parent window has abc.xyz.com url and, I am opening a child window using: window.top.open
I am getting parent window object in child window using window.opener and polling the parent window url using window.opener.location.href.
Now, if the user click on a link in the parent window, which navigates to def.xyz.com then the window.opener.location.href is giving 'Permission Denied' and window.opener.closed is returning true. (in child script)
I need to change my child window if the parent window is not inside xyz.com
How do I know that the parent is navigated to xyz.com or some other domain?

You can add some javascript to the parent window that modifies the child when it's URL changes. Using the window.onunload event would do the job:
window.onunload=function(){
childWindow.location.href = 'parentURLChanged.html';
};
Instead of changing the location completely you could also only add a hash to the URL so It would change from child.html to child.html#parentHasChanged.
If you want more complete communication between these two windows I suggest you use the jQuery postMessage Plugin. It allows you to send and receive messages between two Windows or Frames and also works cross-domain.

Something like this:
query = window.parent.location.search.substring(1)
referenced from HERE. This one can also help.

Related

How to protect window.opener.location

I am using window.open to open a new window which will send some message to the parent window using window.opener.postMessage.
This child window URL is arbitrary. I mean there is a textbox where the users can give the URL and it will be opened using window.open.
Although the user is suspected to use the URLs that are supported, it is also possible that some random URL will be used. In this case, I don't want to child window to make any changes to the parent window, especially to the parent location.
How can I avoid this issue? I've tried to use noreferrer,noopener; however, that breaks entire cross-window communication altogether, which is not desired.

popout browser windows still part of main page

can browser popout windows be launched (like google talk conversation windows can be popped out into a new window from the main gmail page.) so they are still part of the main page i.e. share resources and access and modify each other, e.g. if the popout window contained a button it can be clicked and modify part of the original page without having to go to the server? I'm guessing the answer is no, but as I don't know for sure I thought I'd ask.
You can access the popout parent by using the window.opener property from within the 'popout' window. That will give you the window object of the parent.
So if on your main page you have a global variable test
var test = 'Hello';
It can be accessed from the child window using
window.opener.test

Window.opener with child window navigate in same window not working

I have scenario like P is parent page and from that page one child page is being open using window.open(C) where C is child page. Now I have to navigate in same child window. Let's call that page as C1.
I found when I try to access one button of page P from page C1, it gave me not found.
My syntaxt is like (in page C1),
window.opener.document.getElementById('xyz');
Is there any way using which I can identify the page I am getting in window.opener is same page what I am looking for?
You ask:
Is there any way using which I can identify the page I am getting in window.opener is same page what I am looking for?
Just an idea: generate some random value before opening the new window and store it in the parent window, write this value to the new window and cross-reference.
Update:
You can share data between parent and child browser windows at some scenarios as shown below:
window.opener.functioname(arg1,arg2,..)
- to send data to the parent window from child window
winobj.functioname(arg1,arg2,..)
- to send data to the child window from parent window using the window handle got from window.open()
See for a lot more info on this page.
Good luck!
I am using FCKEditor on my parent page and child page opened from that editor only. So I have to have use window.opener.parent.my_function();
Thanks a lot to all who helped me.

window.opener is null after redirect

I am opening a paypal window from the parent with window.open(). After payment and redirecting back to my page (in the popup window), I would like to close the popup and update the parent window URL.
I found out this works over window.opener.location.
However the console shows
window.opener.location is null
after redirection because as the child window changes, the popup looses the information about the opener.
Well great. Now is there any way to get around this? Maybe adding a sort of "listener" to the parent who listens to the URL of the child?
window.opener is removed whenever you navigate to a different host (for security reasons), there is no way around it. The only option should be doing the payment in a frame if it is possible. The top document needs to stay on the same host.
First you can have a timer function in the parent windows to check whether the child window is opened or closed at particular time interval say 100ms or so. If it is closed then you can reload the parent window.
The issue with window.opener in IE is when you using localhost site and the internet site like paypal. Simply change location of your local host from Local Intranet to the Internet zone and the opener will not be null.

Iframe/Popup redirecting opener window

I have a page located at x.com. On this page is a button that, when clicked, will launch a new window (using javascript's window.open() method) to a page that is located at z.com. The popup does a few things, then redirects the original window (the opener, x.com) to a different page based on some parameters defined in the popup.
This works fine in Firefox/Chrome, but not in IE. In IE (8 specifically, but I believe 7 also has this problem) the original window (the opener) is not redirected. Instead, a new window comes up and THAT window is redirected.
I've tried many different methods to try and get this to work, including changing the popup to an iframe loaded on the page and having a function on the opener that the popup/iframe call. The problem seems to be that IE refuses to allow cross-domain sites to talk to each other via javascript.
Is there a way around this? How can I get the parent window to redirect to a page based on parameters in a popup or iframe?
EDIT:
Here is some code for samples:
In a page on domainA.com, I have this:
<img src='/images/test.png' onclick="window.open('http://www.domainB.com/item.aspx', 'name', 'width=100,height=100,menubar=no,status=no,toolbar=no');" />
In item.aspx on domainB.com I have this in the javascript:
opener.location.href = 'http://www.somethingelse.com/';
In Firefox/Chrome, this works fine. In IE, when domainB.com tries to set location.href on opener (aka the parent window, which is domainA.com), it instead opens a new window, which is not what I want. I want it to redirect the opener (parent window) to the URL I specified.
Bara
Hi I solved my problem by doing the following
instead of using window.opener.location = "....
Use window.opener.document.location = "url". This worked for me.
Another thing is make sure that you are not redirecting from http into https this will also cause it to break.
Cheers
I ended up resolving it by doing the following:
I added an iFrame to my main page. The iFrame is in the same domain as my popup. The iFrame contains a button that, when clicked, will launch the popup.
The popup does it's thing, then changes the iFrame's hash tag to something like #change (so the url would be www.whatever.com/iframe.aspx#change). In the iFrame's javascript, I have a loop going that checks the hash to see if it says "change" and if so, it will redirect the parent page to wherever I want. This works beautifully.
Because I did not want the infinite loop on every single page, I do a browser check so that this only applies to IE. For all other browsers I just use window.opener which works fine.
Bara

Categories

Resources