Rectangle 27 33

If I were you I would check out window.postMessage. It may do what you want:

This is the correct answer. Cross-domain js calls between windows (or iframes) are now possible in HTML5 using Window.postMessage.

Except on IE < EDGE, when using in windows/frames across domains.

javascript - Access parent window from iframe (cross-domain) - Stack O...

javascript html iframe cross-domain
Rectangle 27 32

If I were you I would check out window.postMessage. It may do what you want:

This is the correct answer. Cross-domain js calls between windows (or iframes) are now possible in HTML5 using Window.postMessage.

Except on IE < EDGE, when using in windows/frames across domains.

javascript - Access parent window from iframe (cross-domain) - Stack O...

javascript html iframe cross-domain
Rectangle 27 9

If I understand correctly, all modern browsers do now allow to do this. So I'm here to find the best solution.

This is your solution. What you're asking is not possible.

As mentioned in the comments below, @JeremysAwesome's answer offers a method that would allow cross-domain requests under certain circumstances. See the SO question below for more information.

I understand that this is impossible to manipulate parent window from child window. What I'm looking for is how to imitate this manipulation. In other words, some flag from child window on which parent window will run some piece of code, will do.

@Malgin, again, this is not possible. You're asking for script level interaction between the pages. This is not possible. You cannot imitate something that cannot be done. There are lots of questions on SO that deal with this. See edit.

The only thing that was not clear to me from you answers is that it is impossible to manipulate child window from parent window either. That was my thought that made me be more persistant. Thanks again.

You can, however, change the window location. If you know the URLs in question and have control of both domains you could probably use query strings to send messages between the two. Sounds like you should pursue a better solution though.

As others have pointed out, this actually is possible, via postMessage for example. So @jeremysawesome answer should be the accepted one.

javascript - Access parent window from iframe (cross-domain) - Stack O...

javascript html iframe cross-domain
Rectangle 27 9

If I understand correctly, all modern browsers do now allow to do this. So I'm here to find the best solution.

This is your solution. What you're asking is not possible.

As mentioned in the comments below, @JeremysAwesome's answer offers a method that would allow cross-domain requests under certain circumstances. See the SO question below for more information.

I understand that this is impossible to manipulate parent window from child window. What I'm looking for is how to imitate this manipulation. In other words, some flag from child window on which parent window will run some piece of code, will do.

@Malgin, again, this is not possible. You're asking for script level interaction between the pages. This is not possible. You cannot imitate something that cannot be done. There are lots of questions on SO that deal with this. See edit.

The only thing that was not clear to me from you answers is that it is impossible to manipulate child window from parent window either. That was my thought that made me be more persistant. Thanks again.

You can, however, change the window location. If you know the URLs in question and have control of both domains you could probably use query strings to send messages between the two. Sounds like you should pursue a better solution though.

As others have pointed out, this actually is possible, via postMessage for example. So @jeremysawesome answer should be the accepted one.

javascript - Access parent window from iframe (cross-domain) - Stack O...

javascript html iframe cross-domain
Rectangle 27 9

postMessage is probably what you're looking for. Mozilla has documented this and it has fairly decent cross browser support:

From here you'll need an event listener on the parent window to handle all incoming requests ... which will remove the iframe from the parent context. Here is an example of registering the message received event.

function registerWindowHandler() {
    if (typeof window.addEventListener !== 'undefined') {
    window.addEventListener('message', receiveMessage, false);
    } else {
    // Support for ie8
    window.attachEvent('onmessage', receiveMessage);
    }
}

Hi Travis, Thanks. I did already have postMessage in place. What I had forgotten to do was to add the event listener to the parent frame. Which is why my posts from the parent to the child were working, but not the other way round.

Javascript communicating cross-domain to parent window of iframe - Sta...

javascript iframe cross-domain
Rectangle 27 8

Your title mentions a child window. If you have a child window, and not an iframe, use this:

window.opener.events_data
window.opener

Your code indicates that you're using an iframe. From an iframe, simply use parent:

parent.events_data;
window.parent

window.opener - Returns a reference to the window that opened this current window.

window.parent - When a window is loaded in an , , or , its parent is the window with the element embedding the window.

And what about permission deniel ? If browser doesn't allow to access parent properties from iframe ?

If I'm not mistaken, this happens when the contents of iframe is loaded from another domain

@Malgin, you're correct. Cross domain scripting is a huge security risk and is therefore not allowed. You can tell that the OP is not attempting to do that because the src attribute of his iframe is using a relative path.

@JamesHill All that is true, but if this is required ? Here's my question and the way I'm going to do that in my app (click)

Access parent object in JavaScript from iFrame/Window - Stack Overflow

javascript iframe parent-child
Rectangle 27 5

Instead of using the referrer, you can implement window.postMessage to communicate accross iframes/windows across domains. You post to window.parent, and then parent returns the URL. This works, but it requires asynchronous communication. You will have to write a synchronous wrapper around the asynchronous methods, if you need it synchronous.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>

    <!--
    <link rel="shortcut icon" href="/favicon.ico">


    <link rel="start" href="http://benalman.com/" title="Home">

    <link rel="stylesheet" type="text/css" href="/code/php/multi_file.php?m=benalman_css">

    <script type="text/javascript" src="/js/mt.js"></script>
    -->

    <script type="text/javascript">
        /*
        // Opera 9 supports document.postMessage() 
        // document is wrong
        window.addEventListener("message", function (e) {
            //document.getElementById("test").textContent = ;
            alert(
                e.domain + " said: " + e.data
                );
        }, false);
        */

        // https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage
        // http://ejohn.org/blog/cross-window-messaging/
        // http://benalman.com/projects/jquery-postmessage-plugin/
        // http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html

        // .data  A string holding the message passed from the other window.
        // .domain (origin?)  The domain name of the window that sent the message.
        // .uri  The full URI for the window that sent the message.
        // .source  A reference to the window object of the window that sent the message.
        function ReceiveMessage(evt) {
            var message;
            //if (evt.origin !== "http://robertnyman.com")
            if(false)
            {
                message = 'You ("' + evt.origin + '") are not worthy';
            }
            else
            {
                message = 'I got "' + evt.data + '" from "' + evt.origin + '"';
            }

            //alert(evt.source.location.href)

            var ta = document.getElementById("taRecvMessage");
            if(ta == null)
                alert(message);
            else
                document.getElementById("taRecvMessage").innerHTML = message;

            // http://javascript.info/tutorial/cross-window-messaging-with-postmessage
            //evt.source.postMessage("thanks, got it", evt.origin);
            evt.source.postMessage("thanks, got it", "*");
        } // End Function ReceiveMessage




        if (!window['postMessage'])
            alert("oh crap");
        else {
            if (window.addEventListener) {
                //alert("standards-compliant");
                // For standards-compliant web browsers (ie9+)
                window.addEventListener("message", ReceiveMessage, false);
            }
            else {
                //alert("not standards-compliant (ie8)");
                window.attachEvent("onmessage", ReceiveMessage);
            }
        }
    </script>


</head>
<body style="background-color: gray;">
    <h1>Test</h1>

    <textarea id="taRecvMessage" rows="20" cols="20" ></textarea>

</body>
</html>

The loading of the child iframe is not going to work in a mixed-mode environment. For example, the main page in https and the child page in an iframe (http).

@lmiguelmh: Correct - if it would, that would be a security bug and the browser would need fixing. What you could do in this scenario is A) Putting the child page in https, B) form-post from the iframe to the https page, on the https page you have web-sockets that get notified if a form-post arrives on the server. Alternatively poll in an interval of x seconds whether new data has arrived. If you assign the post a guid, then the child can JSONP-poll the https page (that should be allowed), asking for the answer by guid - poll until it gets it. Don't know if web-sockets could beUsed inThatCase.

How do I implement Cross Domain URL Access from an Iframe using Javasc...

javascript iframe cross-domain
Rectangle 27 5

Instead of using the referrer, you can implement window.postMessage to communicate accross iframes/windows across domains. You post to window.parent, and then parent returns the URL. This works, but it requires asynchronous communication. You will have to write a synchronous wrapper around the asynchronous methods, if you need it synchronous.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>

    <!--
    <link rel="shortcut icon" href="/favicon.ico">


    <link rel="start" href="http://benalman.com/" title="Home">

    <link rel="stylesheet" type="text/css" href="/code/php/multi_file.php?m=benalman_css">

    <script type="text/javascript" src="/js/mt.js"></script>
    -->

    <script type="text/javascript">
        /*
        // Opera 9 supports document.postMessage() 
        // document is wrong
        window.addEventListener("message", function (e) {
            //document.getElementById("test").textContent = ;
            alert(
                e.domain + " said: " + e.data
                );
        }, false);
        */

        // https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage
        // http://ejohn.org/blog/cross-window-messaging/
        // http://benalman.com/projects/jquery-postmessage-plugin/
        // http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html

        // .data  A string holding the message passed from the other window.
        // .domain (origin?)  The domain name of the window that sent the message.
        // .uri  The full URI for the window that sent the message.
        // .source  A reference to the window object of the window that sent the message.
        function ReceiveMessage(evt) {
            var message;
            //if (evt.origin !== "http://robertnyman.com")
            if(false)
            {
                message = 'You ("' + evt.origin + '") are not worthy';
            }
            else
            {
                message = 'I got "' + evt.data + '" from "' + evt.origin + '"';
            }

            //alert(evt.source.location.href)

            var ta = document.getElementById("taRecvMessage");
            if(ta == null)
                alert(message);
            else
                document.getElementById("taRecvMessage").innerHTML = message;

            // http://javascript.info/tutorial/cross-window-messaging-with-postmessage
            //evt.source.postMessage("thanks, got it", evt.origin);
            evt.source.postMessage("thanks, got it", "*");
        } // End Function ReceiveMessage




        if (!window['postMessage'])
            alert("oh crap");
        else {
            if (window.addEventListener) {
                //alert("standards-compliant");
                // For standards-compliant web browsers (ie9+)
                window.addEventListener("message", ReceiveMessage, false);
            }
            else {
                //alert("not standards-compliant (ie8)");
                window.attachEvent("onmessage", ReceiveMessage);
            }
        }
    </script>


</head>
<body style="background-color: gray;">
    <h1>Test</h1>

    <textarea id="taRecvMessage" rows="20" cols="20" ></textarea>

</body>
</html>

The loading of the child iframe is not going to work in a mixed-mode environment. For example, the main page in https and the child page in an iframe (http).

@lmiguelmh: Correct - if it would, that would be a security bug and the browser would need fixing. What you could do in this scenario is A) Putting the child page in https, B) form-post from the iframe to the https page, on the https page you have web-sockets that get notified if a form-post arrives on the server. Alternatively poll in an interval of x seconds whether new data has arrived. If you assign the post a guid, then the child can JSONP-poll the https page (that should be allowed), asking for the answer by guid - poll until it gets it. Don't know if web-sockets could beUsed inThatCase.

How do I implement Cross Domain URL Access from an Iframe using Javasc...

javascript iframe cross-domain
Rectangle 27 2

You have to pass in the context to search in (the parent document)

// Untested
$(window.parent.document, window.parent.document).ready(function () {
    var $scrollingDiv = $("#IframeDir");

    $(window.parent, window.parent.document).scroll(function () {
        $scrollingDiv
        .stop()
        .animate({ "marginTop": ($(window.parent, window.parent.document).scrollTop() + -10) + "px" }, "slow");

    });
});

javascript - Access parent window scroll event from child inside ifram...

javascript jquery asp.net iframe
Rectangle 27 8

parent.document.getElementById('foo').innerHTML = '';
window.parent.document.getElementById('foo').innerHTML = '';

javascript - Access DOM objects of parent window from jQuery DOMWindow...

javascript jquery dom iframe
Rectangle 27 1

I also wanted the floating div within iframe and Rantul this code worked for me!

$(parent.window).scroll(function() {
   //parent document scroll functions go here
});

javascript - Access parent window scroll event from child inside ifram...

javascript jquery asp.net iframe
Rectangle 27 4

I see you have jquery as tag so you can do this as below.

window.opener.document.$("#foo")
$( "#foo", window.opener.document)

javascript - Access DOM objects of parent window from jQuery DOMWindow...

javascript jquery dom iframe
Rectangle 27 95

This is true if a window is not a frame/iframe:

if(self==top)

If you like to see if the parent window of the given window is a frame, use:

if(parent==top)

It's a simple comparision of top (the most top window of the window hierarchy) and another window object (self or parent).

if you like to check if the parent window is within an frame, just use if(parent==top) (will return true if the parent window is not a frame)

javascript - Check if parent window is iframe or not - Stack Overflow

javascript jquery iframe
Rectangle 27 5

Yes you can. You can implement window.postMessage to communicate accross iframes and/or windows across domains. But you need to do it in an asynchronous way. If you need it synchronously, you need to implement wrappers around those asynchronous methods.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>

    <!--
    <link rel="shortcut icon" href="/favicon.ico">


    <link rel="start" href="http://benalman.com/" title="Home">

    <link rel="stylesheet" type="text/css" href="/code/php/multi_file.php?m=benalman_css">

    <script type="text/javascript" src="/js/mt.js"></script>
    -->

    <script type="text/javascript">
        /*
        // Opera 9 supports document.postMessage() 
        // document is wrong
        window.addEventListener("message", function (e) {
            //document.getElementById("test").textContent = ;
            alert(
                e.domain + " said: " + e.data
                );
        }, false);
        */

        // https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage
        // http://ejohn.org/blog/cross-window-messaging/
        // http://benalman.com/projects/jquery-postmessage-plugin/
        // http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html

        // .data  A string holding the message passed from the other window.
        // .domain (origin?)  The domain name of the window that sent the message.
        // .uri  The full URI for the window that sent the message.
        // .source  A reference to the window object of the window that sent the message.
        function ReceiveMessage(evt) {
            var message;
            //if (evt.origin !== "http://robertnyman.com")
            if(false)
            {
                message = 'You ("' + evt.origin + '") are not worthy';
            }
            else
            {
                message = 'I got "' + evt.data + '" from "' + evt.origin + '"';
            }

            //alert(evt.source.location.href)

            var ta = document.getElementById("taRecvMessage");
            if(ta == null)
                alert(message);
            else
                document.getElementById("taRecvMessage").innerHTML = message;

            // http://javascript.info/tutorial/cross-window-messaging-with-postmessage
            //evt.source.postMessage("thanks, got it", evt.origin);
            evt.source.postMessage("thanks, got it", "*");
        } // End Function ReceiveMessage




        if (!window['postMessage'])
            alert("oh crap");
        else {
            if (window.addEventListener) {
                //alert("standards-compliant");
                // For standards-compliant web browsers (ie9+)
                window.addEventListener("message", ReceiveMessage, false);
            }
            else {
                //alert("not standards-compliant (ie8)");
                window.attachEvent("onmessage", ReceiveMessage);
            }
        }
    </script>


</head>
<body style="background-color: gray;">
    <h1>Test</h1>

    <textarea id="taRecvMessage" rows="20" cols="20" ></textarea>

</body>
</html>

Here, you would modify the child to send postmessages to parent. e.g. in child.htm, you do

window.parent.postMessage("alert(document.location.href); document.location.href = 'http://www.google.com/ncr'", "*");

and in parent, you do (in receiveMessage) eval(evt.data); Not that using eval is insecure, so you would instead pass an enum, and call the corresponding function that you need to put on the parent page.

The loading of the child iframe is not going to work in a mixed-mode environment. For example, the main page in https and the child page in an iframe (http).