Web Analytics Made Easy -
StatCounter parent.document issue... - CodingForum

Announcement

Collapse
No announcement yet.

parent.document issue...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • parent.document issue...

    I've seen this question (well, almost this question) asked a million times on web, but none of the solutions I've tried actually work...

    I have a html document that has an iFrame inside of it.
    Inside the iFrame web page, there is a javascript function.


    iFrame HTML:

    <html>
    <script type="text/javascript">
    function getElements(){var x=parent.document.getElementsByTagName("html")[0].innerHTML;alert(x);}
    </script>

    <body>blah blah blah</body>
    <script type="text/javascript">getElements();</script>
    </html>


    I want to get the HTML code of the iFrame's parent. The JS function cannot exist in the parent. It has to exist in the child.

    "x=document.getElementsByTagName("html")[0].innerHTML;" works just fine for showing the html text of the iFrame page itself, but obviously not for the iFrame's parent HTML page.

    "x=parent.document.getElementsByTagName("html")[0].innerHTML;" does not seem to do anything. The alert never shows up. Both web pages are in the same folder, so there shouldn't be any security issues...




    Ideas?

  • #2
    Isn't is supposed to be "document.parent" rather than "parent.document?"

    I honestly don't mess around with iframes so I'm not going to be of much use for this, but I'm pretty sure you have that part is backwards. Fixing that might solve the problem.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

    Comment


    • #3
      I tried document.parent too.

      I'm sure I'm doing something wrong with the syntax or missing something here.

      I've tried things like... document.parent.frame... I've tried like 15 combinations of things, and I can't get any of them to work.

      Comment


      • #4
        First of all if the iframed page is loaded from a different domain than the parent - there is now way to do what you want to because its a security issue blocked by most of modern browsers.

        Otherwise try this code for your iframed page:
        Code:
        <html>
        <head>
        <script type="text/javascript">
        	function getParentHTML(){
        		if(!window.parent)
        			alert("Sorry, no parent");
        		if(parent.document && parent.document.documentElement)
        			alert(parent.document.documentElement.innerHTML);
        		else
        			alert("Cannot get parent's HTML");
        	}
         </script>
        </head>
        <body>blah blah blah
        <script type="text/javascript">getParentHTML();</script>
        </body>
        </html>
        Descript.org - lightweight JavaScript foundation framework

        Comment


        • #5
          Originally posted by desiter View Post
          First of all if the iframed page is loaded from a different domain than the parent - there is now way to do what you want to because its a security issue blocked by most of modern browsers.

          Otherwise try this code for your iframed page:
          Code:
          <html>
          <head>
          <script type="text/javascript">
          	function getParentHTML(){
          		if(!window.parent)
          			alert("Sorry, no parent");
          		if(parent.document && parent.document.documentElement)
          			alert(parent.document.documentElement.innerHTML);
          		else
          			alert("Cannot get parent's HTML");
          	}
           </script>
          </head>
          <body>blah blah blah
          <script type="text/javascript">getParentHTML();</script>
          </body>
          </html>

          Very interesting...

          It fails only locally on Google Chrome.
          That code works online.

          Thanks a bunch!

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎