简体   繁体   中英

Insert a js on head of cross-domain iframe content

my problem is: I have some tabs with a dropdown menu,each tab has an iframe: 我的标签

When the user clicks the dropdown menu I need to run some js on iframe content,then I thought that the correct thing to do is this js in my html page:

    var scriptTag = "<script>alert("blabla");<"; // saving script to run
    scriptTag += "/script>"; //add this for fixing the script tag problem
    $("#myframeid").contents().find("head").append(scriptTag); //appending the script on the head of iframe

But when I run it, the console reports like:

Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin " http://localhost:8080 " from accessing a frame with origin " http://www.otherdomainsite.com ". Protocols, domains, and ports must match.

Obviously if it is on a different domain it does not run,so what I have to do now? Am I doing something wrong?

I heard about postMessage ecc.. methods but I never used them.

Thank you in advance.

You are not allowed to execute code against an external domain IFRAME.

The only way, would be to have proxy iFrame for each direction of message passing.

ternarylabs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM