0

I have an iframe which is generated on the fly and appended in a div like this:

content.innerHTML = '<iframe src="' + url + '" id="id_iframe" style="border:0px #BD8D46 dotted;" scrolling="auto" frameborder="no" align="center"></iframe>'; 

The div which is appended in invisible (display: none;)

What I want to do is to change the display of the div according to the content of the iframe.

I try this

console.log($("#id_iframe").contents()); 

But I get nothing back. No contents. When I use the developers tools I see that the iframe exists in the DOM.

Is it possible to get the iframe content on the fly?

7
  • 1
    Is it from same domain as the main page? If not, you can't access it. If it is then use a load event Commented Nov 14, 2017 at 12:45
  • I think this must be the issue. That it doesn't come from the same domain page. Commented Nov 14, 2017 at 12:47
  • 1
    Nothing you can do inside the frame due to "same origin policy" Commented Nov 14, 2017 at 12:49
  • 1
    The only way you can do this is by creating some server-side code to download the page for you, which means it's served by the same domain when you request it. Do you have any server-side coding knowledge, like PHP or ASP.Net? Commented Nov 14, 2017 at 12:53
  • Thanks @Archer. Yes. I think I will have to do that. Actually the issue is cause I am working on my local development env and at the same time displaying data from a remote server (through geoserver). Once the whole app is transfered to the production should be NO problem. Commented Nov 14, 2017 at 13:04

1 Answer 1

1

$("#id") can not select dom elemets that where added after dom load

$("document").find("#id_iframe").contents()

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.