www.webdeveloper.com
Results 1 to 13 of 13

Thread: iFrame content....

  1. #1
    Join Date
    Aug 2008
    Posts
    61

    iFrame content....

    Alright I'm completely stumped.
    I have this:
    Code:
    getstats = document.getElementById("arcadeStats");
    workWith = getstats.contentWindow.document.body.innerHTML;
    Now my problem is workWith is an empty string. However, getstats.contentWindow.document.body.innerHTML; returns the content of the frame just fine. I mean what could be going wrong here?

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    If practical, why not post all your code instead of just 2 lines because I can't see anything wrong in those 2 lines, unless element "arcadeStats" does not exist, and so the problem lies elsewhere.

  3. #3
    Join Date
    Aug 2008
    Posts
    61
    Code:
    <iframe src='http://s1.zetaboards.com/babtest/pages/arcade' id='arcadeStats' style='display: none;'></iframe>
    <script type='text/javascript'>
    	getstats = document.getElementById("arcadeStats");
    	workWith = getstats.contentWindow.document.body.innerHTML;
    </script>

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    I'm getting "access Denied" messages in IE and FF when I use your iframe's src or any other url outside of my domain when I try to get the code for the iframe's src.

    It works fine when the iframe src is a html file in my domain.

  5. #5
    Join Date
    Aug 2008
    Posts
    61
    Well its on the same server and loads up fine when used on my page

  6. #6
    Join Date
    Mar 2010
    Posts
    2,803
    The iframe's src urls load into my iframe as well, but when I run the code to get the iframe's innerHTML I get the "access denied" message if the url src is outside of my domain. If the src is inside my domain, I can alert() the iframe's innerHTML without a problem.

  7. #7
    Join Date
    Aug 2008
    Posts
    61
    Yes I can alert the innerHTML as well however setting it to the workWith variable doesn't work it just sets workwith as a empty string

  8. #8
    Join Date
    Mar 2010
    Posts
    2,803
    This is the function I am using for testing:

    Code:
    function getContents() {
       var iframeO = document.getElementById("ifrAns");  
       var content = iframeO.contentWindow.document.body.innerHTML; 
       
       alert(content);
    }
    When the iframe's src is inside my domain the alert works without a problem.

    When the src is outside my domain I get either an "access denied" pop up in IE or an empty alert box in FF.

    Maybe it's a system thing - I don't know. I don't use iframes because they are not XHTML Strict compliant and so unfortunately I can't help much more. Hopefully one of the gurus will be able to help you.

  9. #9
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by tirna View Post
    Maybe it's a system thing - I don't know.
    It is a security matter. Javascript relationship between documents belonging to different domains are forbidden by default, for obvious security reasons. Moreover, browsers interpreters are instructed to consider even the sub-domains as different domains.

  10. #10
    Join Date
    Sep 2009
    Posts
    146
    Kor is right, I remember years ago trying to get a specific value form an iframe that was off a different domain. Its been too long and I cant remember if I was successful but I do remember finding a jquery iframe plugin that got me very close… may be worth a google search on your end

    Depending on what you are trying to get from the iframe, worst case you could use an ajax call to a php page that pulls the contents for your manipulation (as needed).

    Good luck,
    -aPeg

  11. #11
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by apeg View Post
    but I do remember finding a jquery iframe plugin that got me very close…
    No way. JQuery is nothing but a JavaScript library. JQuery is not another language. It's just sort of framework. JavaScript written. Like many others (Prototype, MooTools, ...)

    Accessing a document from another domain is a server-side job, exclusively. To do that job asynchronously, AJAX + server-side application is possible.
    Last edited by Kor; 04-09-2010 at 03:34 PM.

  12. #12
    Join Date
    Aug 2008
    Posts
    61
    The website in the iframe is in the domain. As I've said already.... So please stop telling me its because its on a different domain

  13. #13
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Then give us more details. More code. A link to a test page will be of great help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles