How to get a page title which in iframe?

by wilmer.lemke , in category: Javascript , 8 months ago

How to get a page title which in iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by gilbert , 8 months ago

@wilmer.lemke 

To access the title of a page within an iframe, you can use the following script:

1
2
3
4
5
6
7
8
9
// Replace 'iframeId' with the ID or name of your iframe element
var iframe = document.getElementById('iframeId');

// Access the contentWindow property of the iframe
var iframeWindow = iframe.contentWindow;

// Get the title of the page within the iframe
var pageTitle = iframeWindow.document.title;
console.log(pageTitle);


Make sure that you replace 'iframeId' with the actual ID or name attribute of your iframe element. This code retrieves the iframe element using its ID, then accesses the contentWindow property to grab the window object of the page within the iframe. Finally, the script retrieves the title of the iframe's page using iframeWindow.document.title and logs it to the console.