1

I know how to load an html page in another html page (multiple methods, javascript, jquery, html5) and I know how to post login information on an html page and submit, but here is the problem. When I load the original html page in another html page, I cannot access the form since it is in an object tag. Here is how I load the html page:

var URL = http://sampleurl.com/testpage
document.getElementById("testDIV").innerHTML = '<object type="text/html" data="'+URL+'" width="100%" height="100%"></object>';

Now I am left with something like this:

<iframe class="container-frame" name="test" src="test" data-loaded="1">
    #document
    <html>
        <head>...</head>
        <body>
          ...
          <form>...</form>
          ...
        </body>
    </html>
</iframe>

I am unable to access the body inside the iframe using javascript to have information posted to a form in that body.

I have tried stuff like:

document.getElementsByTagName("html")[0].childNodes...

But I cannot get to the inner form element nor the body surrounding it. Is there a way that this can be done?

  • is the second page in same domain than first? If not you'll have a CORS error and not possible to access to the content on the iframe. Assuming you don't have this problem, you could do: var iframe = document.getElementById(yourIframeID'); var content = iframe.contentDocument || iframe.contentWindow.document; see https://stackoverflow.com/questions/1088544/get-element-from-within-an-iframe – charly3pins Sep 28 '17 at 15:30
  • @charly3p The CORS issue is not present. It is taken care of by my nginx setup. This does not work. The problem is this: var iframe = document.getElementById("myID"); contains a
    with inside it and that object has the #document. So iframe.contentDocument || iframe.contentWindow.document is returning the error: Uncaught TypeError: Cannot read property 'document' of undefined
    – Mohammad Etemad Sep 28 '17 at 15:37
  • Why are you using ? Why not just create the iframe and load the properties directly on the DOM? – Carlos Alves Jorge Sep 28 '17 at 15:50
  • https://stackoverflow.com/questions/8726455/creating-an-iframe-using-javascript – Carlos Alves Jorge Sep 28 '17 at 15:51
  • @CarlosAlvesJorge It does matter what I use. Anything that I sue will end up with #document inside it which makes it impossible to get to. – Mohammad Etemad Sep 28 '17 at 15:54
  • @MohammadEtemad if you can, put your code in a jsbin or jsfiddle and I'll check it – charly3pins Sep 28 '17 at 20:35

0 Answers0