0

I am using a third party library that mandates an iframe with some id, say "x", to load content from their site. And once the content is loaded, the HTML that comes from their site contains a div with the same id - "x". I can not change the iframe id because they need it that way and I have no control over the div's id. How can get to the iframe using native javascript? Will document.getElementById("x") always return the iframe because that's the first element in the dom?

Thanks for the help.

legendofawesomeness
  • 2,901
  • 2
  • 19
  • 32
  • The library mandating duplicate `id`s means it was poorly written. I'd dump it if I could and either write the code yourself or find something another library that does what you're trying to do. – Dennis Feb 03 '12 at 19:55
  • Thanks Dennis. I wish I had that choice..unfortunately not! :( – legendofawesomeness Feb 03 '12 at 19:59

2 Answers2

1

An iframe served from a different origin (domain) will create a nested browsing context then its content cannot be inspected from your script and so there is no way to accidentally select the id from inside.

In the case of an iframe from the same origin as the parent document, you may need to explicitly get the iframe contents first and then search that if you want to find the element by that id inside the iframe. Here is an example. The jQuery function '.content()' makes this convenient.

Community
  • 1
  • 1
tilgovi
  • 306
  • 2
  • 11
0

hope, this code would be of any help :)

var frameTag=document.getElementById(iframeid);
     if (frameTag.tagName=='IFRAME')
     {
        //execute your conditions
     }
Lokesh Yadav
  • 1,574
  • 5
  • 23
  • 50
  • Thanks Lokesh. I also tested that document.getElementById() returns the first element that matches the Id, which in my case will always be the iframe. – legendofawesomeness Feb 03 '12 at 20:04