Cross domain iframe issue

前端 未结 5 1730
[愿得一人]
[愿得一人] 2020-11-21 06:14

For say i have a Site called example.com on which iframe is embedded of domain iframe.net, now i want to read the content of iframe and pass some parameter to display a text

5条回答
  •  梦毁少年i
    2020-11-21 07:06

    If you don't have control over the framed site, you cannot circumvent the cross-domain policy.

    If you have control over both sites, you can use the postMessage method to transfer data across different domains. A very basic example:

    // framed.htm:
    window.onmessage = function(event) {
        event.source.postMessage(document.body.innerHTML, event.origin);
    };
    
    // Main page:
    window.onmessage = function(event) {
        alert(event.data);
    };
    
    // Trigger:
    // 
    document.getElementById('myframe').contentWindow.postMessage('','*');
    

提交回复
热议问题