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 ?
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.
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.
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.
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.
You can try :
$("#test")[0].contentWindow.variable;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With