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:
MY TABS

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.


Source: javascript

Leave a Reply