0

...but I do control part of the javascript inside the second domain (which integrates the iframe). So, what I need is some workaround my problem. We have example2.com (this one holds the iframe) and example.com (this one is the original, within the iframe). Inside the iframe the user clicks a button that executes parent.redirectUser() and although I have that function defined in example2.com it fails to execute because it points the function as forbidden to access from within the iframe. Considering I can control the javascript in example2.com, is there any other way to workaround this situation? Thank you very much for your help...

Fane
  • 1,978
  • 8
  • 30
  • 58

1 Answers1

0

Yes, you can do it. You can use messages technique, send message from child(example2.com):

parent.postMessage("Ok", "example.com");

in parent(example.com) you must add the following code:

    //add eventlistener for message event
    if (window.addEventListener) {
        window.addEventListener("message", listener);
    } else {
        // IE8
        window.attachEvent("onmessage", listener);
    }
    function listener(event) {
//check the message trust or not?
        if (event.data == "Ok") {
//from parent you can call function, but function must be placed in global scope
            redirectUser();
        }
    }
antoniOS
  • 370
  • 2
  • 9
  • I don't understand exactly how that terminology "opener" works though... I'm calling that line from within the iframe right? What exactly should I put inside "opener" then? – Fane Nov 09 '15 at 21:59
  • opener is the parent window, yes that line you call from iframe, and into opener window you must put code with eventlistener. – antoniOS Nov 09 '15 at 22:20
  • So something like `opener = getParentContainer()`? – Fane Nov 09 '15 at 22:41
  • opener is a global variable places in window that refer parent window. – antoniOS Nov 09 '15 at 22:50
  • please read this http://www.w3schools.com/jsref/prop_win_opener.asp The opener property returns a reference to the window that created the window. When opening a window with the window.open() method, you can use this property from the destination window to return details of the source (parent) window. – antoniOS Nov 09 '15 at 22:50
  • From iframe try to use parent or window.parent – antoniOS Nov 10 '15 at 19:44
  • please check http://stackoverflow.com/questions/33630495/postmessage-not-being-received-from-iframe?noredirect=1#comment55034335_33630495, my other question :) – Fane Nov 10 '15 at 21:19