Accessing cookies of an iFrame in parent window

后端 未结 2 1172
说谎
说谎 2020-12-08 17:12

I am loading an iFrame of a different domain. Both the parent and the iFrame sites are under my control. I\'m using iFrame.postMessage to post messages to the iFrame. The si

相关标签:
2条回答
  • 2020-12-08 17:55

    I am not sure how are you catching the postMessage on the parent window or even catching or not, but below is the code that you should have on the parent window to catch the postMessage from the child iframe-

    <script>
        window.addEventListener( "clientId",
          function (e) {
                if(e.origin !== 'https://localhost:9443'){ return; } 
                alert(e.data);
          },
          false);
    </script>
    

    UPDATE:
    I replicated your scenario at my end and found that you should use-

    document.cookie.split(';');
    parent.postMessage(clientId,"https://localhost:9443/oauth2/loginstatus");
    

    instead of-

    opIFrame.cookie.split(';');
    opIFrame.postMessage(clientId,"https://localhost:9443/oauth2/loginstatus");
    

    Full Code
    Parent Window: - http://localhost:8541

    <div>
         <h1>Parent Window</h1>
         <iframe src="http://localhost:50761/parent/test" width="100" height="100"></iframe>
         <script>
             window.addEventListener("message",
             function (e) {
                if (e.origin !== 'http://localhost:50761') { return; }
                   alert(e.data);
                },false);
         </script>
    </div>
    

    iFrame Page: - http://localhost:50761

    <div>
        <h1>iFrame Window</h1>
        <script type="text/javascript">
            function createCookie(name, value, days) {
                if (days) {
                    var date = new Date();
                    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                    var expires = "; expires=" + date.toGMTString();
                }
                else var expires = "";
                document.cookie = name + "=" + value + expires + "; path=/";
            }
    
            function readCookie(name) {
                var nameEQ = name + "=";
                var ca = document.cookie.split(';');
                for (var i = 0; i < ca.length; i++) {
                    var c = ca[i];
                    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
                }
                return null;
            }
            createCookie('testCookie', "test content", 1);
            parent.postMessage(readCookie('testCookie'), "http://localhost:8541/");
        </script>
    </div>
    

    In the above code you can see I am accessing the cookie value in cross domain environment i.e. parent window is running on different domain and page loading in iframe is running on different domain.

    I created a test cookie with test data in it and passed to the parent window using postMessage.

    0 讨论(0)
  • 2020-12-08 17:59

    This will give you the cookie of the iframe:

    var cookie = document.getElementById("iframeId").contentDocument.cookie;
    

    To get a cookie by name use this function (from stackoverflow):

    function getCookie(cookie, name) {
        function escape(s) { return s.replace(/([.*+?\^${}()|\[\]\/\\])/g, '\\$1'); };
        var match = cookie.match(RegExp('(?:^|;\\s*)' + escape(name) + '=([^;]*)'));
        return match ? match[1] : null;
    }
    
    0 讨论(0)
提交回复
热议问题