Postmessage example from console cross-domain - javascript

I just try to receive postmessage from other window in my browser.
So I opened 2 sites: example1.com and example2.com. And I opened chrome inspectors on both tabs.
In one inspector I placed code:
window.addEventListener('message', function(){console.log('FIRED')})
In second one:
window.postMessage('Hello!', '*');
No fire if they're different tabs and even same domains. It only fires if it's the same chrome inspector.
So what I do wrong?

You can't arbitrarily post across tabs. The postMessage method is on window's prototype, so you need a valid Window to post against.
If you are posting to the current window's parent or a child (and can use window.parent or get the child frame's context`) or have opened a new window and still hold a reference to it, then you can post across frames or tabs. You cannot just post to an arbitrary tab open in the same browser, however, as that would be a serious security issue.

Related

Passing messages from iframe to parent window (cross-domain)

I need to pass a message from an iframe to the parent window. The parent window and iframe window are from different domains so I am running into difficulty with same origin restrictions (i.e., my attempts to access window.parent from the iframe are blocked).
I have been able to pass messages from the parent window to the iframe using the window.location.hash method as described in this article. However, the method does not appear to work for sending messages from the iframe to the parent window.
I must also find a solution that works in IE7 and IE8.
Does anyone know how to do this?
I'm about to give up and build a web API to facilitate messages from the iframe to the parent window.
Take closer look at window.postMessage API. It allows you to send message across all domains and between iframes. Perfectly works on IE as well

window.opener is useless after a redirect in the popup window (JavaScript)

I'm building an app that involves authentication via third-party. To make the process not redirect the actual app I open a new window that then does the authentication and returns to main window after success.
This doesn't, however, go as well as planned. When the popup redirects to third-party and back, window.opener gets null. It's still possible to close the popup by window.close() but I also need to refresh the logged-in-area in the main window, like this:
window.opener.check_auth_status();
I really hope there is a way to fix this, e.g. binding a function to popup-close in the main window? Refreshing the whole page would be highly unnecessary.
One way is to set an interval to main window checking if the popup is closed, but this seems so fiddly.
You have a few options that may or may not work in the latest versions of the browsers due to security updates
1) check that the window is closed from the opener - not fiddly and actually the safest
2) give the opener a name
window.name="myMainWindow";
and in popup (script from SAME domain) - should normally not open a new window or change content
var handle = window.open("","myMainWindow");
handle.check_auth_status();
3) use an iFrame in the popup and when you want to access the opener, use top.opener

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.

Opening a popup after receving message via postMessage

There is an iframe on my site which is hosted on another domain. It can communicate to the main site via window.postMessage. I do have a button on that iframe which calls a JS method on the main domain, which then triggers a new window.
It all seems good but since the user click event is generated on the iframe domain and window is created on the main domain, browser thinks that this is an automated popup, and blocks.
Is there anything I can do to prevent browser from blocking the window?
since that is browser based you cannot overwrite browser settings. But you can use javascript "modals" which are very useful since they are not pop-ups but they don't have the functionality of an pop-up

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