admin管理员组文章数量:1326682
I main php page in which there are 2 frames. Inside the second frame there is iframe. I want to access value of element on iframe document from the 1st frame.
I tried like this:
var frame1 = parent.frames[1];
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
var eleValue =frame2.contentWindow.document.getElementById("MyElement").value;
but I am not receiving any value though it is there.
var frame1 = parent.frames[1];
alert(frame1.name);
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
alert(frame2.name);
var txtClinic = frame1.document.getElementsByTagName('iframe')[0].contentDocument.getElementById("clinicFlag");
last line of code doesnot return any control object.
I main php page in which there are 2 frames. Inside the second frame there is iframe. I want to access value of element on iframe document from the 1st frame.
I tried like this:
var frame1 = parent.frames[1];
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
var eleValue =frame2.contentWindow.document.getElementById("MyElement").value;
but I am not receiving any value though it is there.
var frame1 = parent.frames[1];
alert(frame1.name);
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
alert(frame2.name);
var txtClinic = frame1.document.getElementsByTagName('iframe')[0].contentDocument.getElementById("clinicFlag");
last line of code doesnot return any control object.
Share Improve this question edited Jun 7, 2014 at 12:08 GotchaRob 4582 silver badges11 bronze badges asked Jun 6, 2014 at 5:20 user3713775user3713775 1683 gold badges4 silver badges14 bronze badges 8- 1 Are both the urls on same domain? – Hanky Panky Commented Jun 6, 2014 at 5:23
- yes both urls are on domain – user3713775 Commented Jun 6, 2014 at 5:24
-
1
One hilarious way to pass information between frames is
window.LocalStorage()
– Scuzzy Commented Jun 6, 2014 at 5:25 - there is any other way to do it? – user3713775 Commented Jun 6, 2014 at 6:03
- This answer should help you, it's not a perfect answer to your question though. – Teemu Commented Jun 6, 2014 at 6:19
1 Answer
Reset to default 5Here's a modified snippet of my answer linked in a ment. Function returns the window
object of an (i)frame with id
passed (id
), or null
, if the (i)frame
is not found. This method works only, if all the (i)frame
s are in the same domain. Also id
s given to (i)frame
elements must be unique throughout all documents involved in the window structure.
function searchFrame(id) { // id = the id of the wanted (i)frame
var result = null, // Stores the result
search = function (iframes) { // Recursively called function
var n; // General loop counter
for (n = 0; n < iframes.length; n++) { // Iterate through all passed windows in (i)frames
if (iframes[n].frameElement.id === id) { // Check the id of the (i)frame
result = iframes[n]; // If found the wanted id, store the window to result
}
if (!result && iframes[n].frames.length > 0) { // Check if result not found and current window has (i)frames
search(iframes[n].frames); // Call search again, pass the windows in current window
}
}
};
search(window.top.frames); // Start searching from the topmost window
return result; // Returns the wanted window if found, null otherwise
}
This function can find a frame
or iframe
with the passed id
regardless where it is placed in the window structure. Also the function can be placed and called in any window. I'd put this to the main page (as global). If the method is needed in subwindows, just call with top.searchFrame(...)
.
Instead of id
s, also name
s can be used, as long as they are also unique. In that case the id
check in searchFrame()
needs to be edited to name
check.
In your case, first you need to give an id
to the target iframe
, then you can use the code for example like this:
var txtClinic = searchFrame('IFRAME_ID').document.getElementById('clinicFlag');
The method above might be a bit overkilling to get a single reference, but it's very helpful, if you have to get these cross-window references multiple times within different windows.
The specific task of yours could be done like this:
var txtClinic = parent.frames[1].frames[0].document.getElementById('clinicFlag');
name
s of the (i)frame
are also handy to use with frames
collection. Instead indices you can use names. Just always chain the reference starting from the main page, i.e. from top
. For example:
var iframeWin = top.frames['frame2'].frames['iframe1'];
Useful reading:
window.top
window.frameElement
window.frames
本文标签: Get element value inside iframe which is nested inside Frame in javascriptStack Overflow
版权声明:本文标题:Get element value inside iframe which is nested inside Frame in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742200780a2431918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论