Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to a variable in an iframe by jquery selector

Tags:

jquery

iframe

I have following:

<iframe id="test">
   <script>
     variable='hi';
   </script>
</iframe>

I need to know how can I access to a variable by jquery selector from the top window like this $("#test").variable ?

like image 444
Ghooti Farangi Avatar asked Jul 11 '11 10:07

Ghooti Farangi


People also ask

Can you access the content of an iframe?

You could use . contents() method of jQuery. The . contents() method can also be used to get the content document of an iframe, if the iframe is on the same domain as the main page.

How do I select an iframe element?

Getting the element in IframegetElementById() method by passing iframe id as an argument. const iframe = document. getElementById("myIframe"); Now, it has and contentWindow property which returns the document object by using that we can access the elements from an Iframe.

Can iframe access parent variables?

parent and to access the child's scope from the parent you can use a couple of methods, such as [url=https://developer.mozilla.org/en/DOM/window.frames]window.frames. So, for instance, if you define a var myvar = 2 in the parent scope, you can access that in the iframe as window. parent. myvar.

Can we access DOM in iframe?

contentWindow. The contentWindow property returns the Window object of an HTMLIFrameElement. You can use this Window object to access the iframe's document and its internal DOM.


1 Answers

You can try :

$("#test")[0].contentWindow.variable;
like image 94
Ankur Avatar answered Oct 14 '22 17:10

Ankur