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