Home > OS >  How to retrieve the title of a web page contained in an iframe?
How to retrieve the title of a web page contained in an iframe?

Time:08-11

I'm working on a solid-js application containing an iframe containing a website of my own design. The website contains links to pages of other domain names. I would like to know if it is possible for me to get the title of the current webpage contained in my iframe. I've read several stack overflow questions saying that it's impossible to get the title of a web page from another domain for security reasons. However, the web pages whose title I am looking for can be loaded in my iframe therefore allow the connection, that's why I wonder if in this case I can get their title. Here is the code of my iframe:

<iframe
  width="100%"
  height="1300"
  id="myiframe"
  src="https://gkwhelps.herokuapp.com"
></iframe>

Please help me, this is urgently needed.

CodePudding user response:

The title of the web page contained in the iframe can be retrieved using the JavaScript function document.getElementById("iframeID").contentWindow.document.title.

CodePudding user response:

$( "iframe" ).on('load',function() {
        document.title = document.getElementById("iframe").contentDocument.title;
});

This waits for the iframe to load then gets the iframe (must set the id to iframe) and then gets the title. Uses jQuery.

CodePudding user response:

You will not be able to do it, unless it is a browser without CORS activated, or the page contained on the IFRAME is on the same domain/server.

You could investigate if you can allow CORS on your page or on the solid-js app.

  • Related