Access children of an iframe from the father of this

2

I have a web which loads a certain iframe, depending on the user wants to access one section or another through the father menu of this iframe.

For everything to work I have to access the elements of said iframe from the father. The problem is that although both are in the same domain (currently I have only tested locally) I can not access the iframe. The way I access is currently invoking functions of the parent, which assign what they receive as an argument to global variables also declared in the parent.

Can anyone help me?

    
asked by jose_smt 18.08.2017 в 20:02
source

1 answer

2

Although the iframes are disused for being a bad practice, you can call functions from the father as follows.

Suppose you have assigned the id < iframe id="myIframe"> to the iframe of which you need to access a function definition within the call say for example myDummyFunction() :

In javascript clean without any addition to call this function would be:

 document.getElementById('myIframe').contentWindow.myDummyFunction();

If you know the order of the Iframe you can also do it in the following way:

// suponiendo que es el primer iframe.
window.frames[0].frameElement.contentWindow.myDummyFunction(); 

Finally with JQuery it would be like this:

$("#myIframe")[0].contentWindow.myDummyFunction();

I hope it will help you even though the recommendation is that you use iframes as little as possible or directly do not use them there is always a better and cleaner way than using them. Greetings

    
answered by 18.08.2017 / 20:25
source