Accessing elements of IFrame..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Accessing elements of IFrame..

    I want to access the elements of IFrame.
    Suppose here is my code ....
    [code=HTML]
    <iframe .... ></iframe>
    [/code]

    Now i want to access the elements inside the IFrame ..how can i do that ?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    For standards-compliant browsers, use contentDocument to get access to the document within the iframe. IE requires contentWindow.d ocument.

    If you use the window.frames[] syntax, you can just use document.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by acoder
      For standards-compliant browsers, use contentDocument to get access to the document within the iframe. IE requires contentWindow.d ocument.

      If you use the window.frames[] syntax, you can just use document.
      Why JavaScript comes up with two flavors?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        No, JavaScript doesn't come with two flavours. Browsers sometimes choose not to implement standards which is where things go wrong and, more often than not, it's IE which is the culprit. There are parts of JavaScript where there are no standards at present. In that case, browsers have to come to some sort of agreement on naming, behaviour, etc.

        Edit: did you mean the two different types of syntax?

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by acoder
          No, JavaScript doesn't come with two flavours. Browsers sometimes choose not to implement standards which is where things go wrong and, more often than not, it's IE which is the culprit. There are parts of JavaScript where there are no standards at present. In that case, browsers have to come to some sort of agreement on naming, behaviour, etc.

          Edit: did you mean the two different types of syntax?
          Sorry you didn't get me ... ;)
          Suppose this is my code ..
          [code=HTML]
          <iframe name='_name' id='_id' ... ></iframe>
          [/code]

          Now i am accessing the document object of IFrame ...
          [code=JavaScript]
          var doc = document.getEle mentById('_id') .contentDocumen t; //Mozilla
          var doc = document.getEle mentById('_id') .contentWindow. document //IE
          var doc = window.frames['_name'].document; //both
          [/code]

          Now my question is ..that
          document.getEle mentById('_id') and window.frames['_name'] refers the same object ..i mean the iIFrame window.

          For one case contentDocument or contentWindow.d ocument and for another case it is cimply document ..why?
          This is my question ?
          I think you get my question ... ;)

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            This link will explain. It boils down to whether it's a frame or an object. The first syntax is used for an object while the second for a frame.

            Comment

            Working...