Accessing cookies of an iFrame in parent window

后端 未结 2 1171
说谎
说谎 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: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;
    }
    

提交回复
热议问题