0

I am trying to add title tags to frames within an iframe. All files reside on the same domain.

The full selector for the html page I am loading within the iframe is "html > frameset:nth-child(2) > frame:nth-child(1)"

However using even the following code does not update the attribute for any frames within the iframe.

$('#project_frame').contents().find('frame').attr('title','Table of Contents');

<iframe id="project_frame" name="project_frame" src="<?= $uri ?>" frameborder="0" title="project_frame"></iframe>
Shawn Dibble
  • 177
  • 2
  • 15

2 Answers2

1

The iframe loading the source is asynchronous to the main execution, so here's a nice trick to make the desired code wait 'til it's loaded:

<iframe id="project_frame" name="project_frame" src="<?= $uri ?>" frameborder="0" title="project_frame"></iframe>

$("#project_frame").load(function () {
    $("#project_frame").contents().find('frame:first').attr('title', 'Table of Contents');
});

Found here: Javascript callback when IFRAME is finished loading?

Community
  • 1
  • 1
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
0
document.getElementById("project_frame").contentWindow.document.title="MY TITLE";
Ryan
  • 14,392
  • 8
  • 62
  • 102
  • From here, how does one access frame elements in the contents of the iframe? Also, there's a typo "getElememtById". – bloodyKnuckles Jun 24 '14 at 01:21
  • Hi. This code may well solve the problem.. but it'd be really nice if you could add a bit of explanation of what exactly you changed from the original and why the difference solves the issue. Don't forget that total newbies are on Stack Overflow all the time - and what may be a really obvious change to you (and why) would not be obvious to them. It's nice to help newbies learn something :) – Taryn East Jun 24 '14 at 01:29