Sometimes, we want to get current URL from an iframe with JavaScript.
In this article, we’ll look at how to get current URL from an iframe with JavaScript.
How to get current URL from an iframe with JavaScript?
To get current URL from an iframe with JavaScript, we can use the contentWindow.location.href
property.
For instance, we write
const url = document.getElementById("iframe_id").contentWindow.location.href;
to select the iframe with
document.getElementById("iframe_id")
Then we get the current URL of the iframe with the contentWindow.location.href
property.
Conclusion
To get current URL from an iframe with JavaScript, we can use the contentWindow.location.href
property.