Currently I have the following in my index.html
file
document.title = document.getElementById('title').contentWindow.document.title;
But it makes the title blank instead of the title of the iframe.
How do I fix this?
Currently I have the following in my index.html
file
document.title = document.getElementById('title').contentWindow.document.title;
But it makes the title blank instead of the title of the iframe.
How do I fix this?
Getting the title
of an iframe
is as easy as:
var iframe = document.getElementById("iframeId");
var iframeTitle = iframe.contentDocument.title;
If you want to get the value of title
when the iframe
is loaded, execute:
document.getElementById("iframeId").onload = function() {
var iframeTitle = document.getElementById("iframeId").contentDocument.title;
}
The title
value is now stored in your iframeTitle
variable.
Afterwards, you can replace you title value on your current page with this variable.
if (document.title != iframeTitle) {
document.title = iframeTitle;
}
If you want to insert this code to your html file, add the following code after your content in your <body>
element.
<script>
document.getElementById("iframeId").onload = function() {
var iframeTitle = document.getElementById("iframeId").contentDocument.title;
if (document.title != iframeTitle) {
document.title = iframeTitle;
}
}
</script>
` tag. If I could help you, it would be nice that you upvote my answer and mark it as the accepted answer.
– Felix Haeberle Dec 09 '17 at 01:54