The src attribute of an iframe element defines the URL of the document that can be displayed in an iframe.
To navigate a URL in an iframe with JavaScript, we need to set the src attribute or pass the value of the src attribute to an iframe element.
document.querySelector('iframe').src = 'url'
The following example will set the iframe src dynamically.
Example : navigate URL in an iframe with JavaScript #
<iframe src="" width="300" height="200"></iframe>
<button onclick="myFunc()">Click Me</button>
<script>
function myFunc(){
document.querySelector('iframe').src='https://www.3schools.in/p/example.html';
}
</script>
Comments (0)