Accessing cookies of an iFrame in parent window

后端 未结 2 1175
说谎
说谎 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-

    
    

    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

    Parent Window

    iFrame Page: - http://localhost:50761

    iFrame Window

    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.

提交回复
热议问题