0

I am trying to grab the content of a div from within an iframe. I have the following code:

$('iframe').load(function() {
  setTimeout(function() {
    content = $("iframe").contents().find("div#totaltime").html();
    alert(content);
  }, 800);
});

My alert message says "undefined" and yet when I remove the ".html()" I get an alert saying "[object Object]" ... any ideas how to extract the contents of this div. There should just be a time in the div like "1:33" but when I run .text() instead of .html() I just get a blank alert.

This is the html from inside the iframe:

<div id="totaltime" class="item graytext" style="visibility: visible; left: 368px; top: 51px; width: 32px; height: 16px; font-size: 11px; font-family: Arial;">06:44</div>

Thanks!

UPDATE

Ok, I've changed to:

$(window).load(function() {
  setTimeout(function() {
    content = $("iframe").html();
    alert(content);
  }, 800);
});

Now I just get a blank alert box.

Abram
  • 39,950
  • 26
  • 134
  • 184
  • Why do you wait for $('iframe') and then read from $("#iFrame") ? – Grzegorz Kaczan Feb 07 '13 at 21:41
  • Sorry, fixed that in my code, but still same problem! – Abram Feb 07 '13 at 21:42
  • You grabbing iframe by tag name, there is only one iframe on a page? But wait $.load method is not for waiting for a iframe to load, but to load external stuff into this tag. See http://api.jquery.com/load/ – Grzegorz Kaczan Feb 07 '13 at 21:47
  • Do you think it has anything to do with http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe – Abram Feb 07 '13 at 21:52

1 Answers1

0

If your parent and child are both in the same domain then, try this (using pure javascript). If they are not in the same domain, for security reasons, it is not possible access contents within an iframe of a different domain;

var iframe = document.getElementById("your_iframeId");
alert(iframe.contentWindow.document.getElementById('totaltime').innerHTML);
Kaf
  • 33,101
  • 7
  • 58
  • 78
  • I'm getting Chrome Extension: Unsafe JavaScript attempt to access frame with URL Domains, protocols and ports must match – Abram Feb 07 '13 at 22:04
  • Ah, to access anything like this both parent and child should be in the same domain – Kaf Feb 07 '13 at 22:05