pass variable to child in javascript?
I want to pass a variable in my parent window to an iframe html document (child).
Example: var myValue = 10;
want to pass myValue
to a child element (hidden input)...
Ho开发者_高级运维w would I do this?
Your question is a bit skimpy on the details, but in principle you can find the IFRAME e.g. with document.getElementById() and then use contentDocument on it to get its document. Again with getElementById() you can then access the desired child element.
Obviously you'd have to give both element "id" attributes.
I think IE does not know contentDocument, so you might have to use contentWindow.document there.
I think this should help you:
http://cross-browser.com/talk/inter-frame_comm.html
From the parent's context: window.frames contains the frame you want. If the frame is in the same domain, use getElementById/getElementsByName to locate your hidden input and assign it the value.
Following on to Steffen's answer, I did something like this earlier today.
if(document.getElementById("foo").document) { // IE
searchableContent = document.getElementById("foo").contentWindow.document;
}
if(document.getElementById("foo").contentDocument) { // Firefox, Chrome
searchableContent = document.getElementById("foo").contentDocument;
}
Object detection might be tangentially of interest to you.
I'm thinking this should work. May need to be tweaked a bit.
var myIframe = document.getElementById('iFrameId');
if ( myIframe.contentDocument ) { // DOM
myIframe.contentDocument.getElementById('hiddenField').value = myValue;
} else if ( myIframe.contentWindow ) { // IE win
myIframe.contentWindow.document.getElementById('hiddenField').value = myValue;
}
精彩评论