Categories
JavaScript Answers

How to get current URL from an iframe with JavaScript?

Spread the love

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.

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 *