Change the URL of the document to embed in the iframe:
document.getElementById("myFrame").src = "https://java2s.com";
Click the button to change the value of the src attribute in the iframe.
<!DOCTYPE html> <html> <body> <iframe id="myFrame" src="/default.asp"></iframe> <button onclick="myFunction()">Test</button> <script> function myFunction() {//w w w . j a v a 2s .co m document.getElementById("myFrame").src = "https://www.java2s.com"; } </script> </body> </html>
The src
property sets or gets the value of the src attribute in an iframe element.
The src attribute specifies the URL of the document to show in an iframe.
Value | Description |
---|---|
URL | Specifies the URL of the document to embed in the iframe. |
The src
property returns a String representing the URL of the document that is embedded in the iframe.