Royi Namir
Royi Namir

Reputation: 148524

Get the document object for an Iframe?

How can I get the document object for an Ifarme ?

I tried this :

$(document,"#IFRM_Quest")[0] //[object Document]

and it works.

But I also tried :

$("#IFRM_Quest").contents()[0] which also yields document. //[object Document]

So why does

$("#IFRM_Quest").contents()[0]===$(document,"#IFRM_Quest")[0]

return false ?

this is suppose to be the same object...

https://i.sstatic.net/NHmlL.png enter image description here

Upvotes: 0

Views: 262

Answers (1)

Felix Kling
Felix Kling

Reputation: 816462

$(document,"#IFRM_Quest")[0] does not select the document object of the iframe, it simply selects the document object of the current, uhm, document.

document is a variable directly referencing to the document object, not a selector. Your function call is equivalent to:

$(document)[0]

because whenever you pass a DOM element to jQuery, the context is ignored:

// HANDLE: $(DOMElement)
} else if ( selector.nodeType ) {
    this.context = this[0] = selector;
    this.length = 1;
    return this;
}

This is actually also shown in the documentation in the function signattures:

jQuery( selector [, context ] ) <- optional context
jQuery( element ) <- no context

Upvotes: 2

Related Questions