Categories
JavaScript Answers

How to get the current location of an iframe with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *