Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get iFrame src content using javascript?

I have an iFrame, content of which is inside another iFrame. I want to get the inside iFrame's src content. How can I do this using javascript?

like image 499
Paulraj Avatar asked Aug 12 '09 06:08

Paulraj


People also ask

How can I access iframe element with JavaScript?

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.

How do I view iframe content?

getIframeContent(frameId): It is used to get the object reference of an iframe. contentWindow: It is a property which returns the window object of the iframe. contentWindow. document: It returns the document object of iframe window.

What is the src attribute in iframe?

The HTML <iframe> src attribute is used to specify the URL of the document that are embedded to the <iframe> element. Attribute Values: It contains single value URL which specifies the URL of the document that is embedded to the iframe.

Is the src property of an iframe can be changed using JavaScript?

The idea behind changing the iframe source is to switch the web page in the display of iframe . In JavaScript we can change the source or redefine it with the JavaScript default method getElementById() . We can re-assign new path or URL of webpage to getElementById("idOfElement"). src .


2 Answers

The outer page:

<html>
<body>
<iframe id="ifr1" src="page1.htm"></iframe>
<iframe id="ifr2" src="http://www.google.com"></iframe>
</body>
</html>

page1.htm:

<html>
<body>
<script type="text/javascript">
document.write(parent.document.getElementById('ifr2').src);
</script>
</body>
</html>
like image 91
Joshua Avatar answered Sep 19 '22 15:09

Joshua


iframeInstance.contentWindow.document - this document loaded from SRC url. and vice verse - to get from inner document to parent use: parent.document

like image 44
Dewfy Avatar answered Sep 19 '22 15:09

Dewfy