Background
I am quite familiar with using javascript to retrieve information from webpages. Typically I can use window.frames["FrameName"].document.getElementById("ElementID");
as a way to get an element within a frame. I have learned that if an element is within a frame then window.document.getElementById("ElementID");
will not work since it needs to look in the frame.
Problem
I have a webpage where it has a frame within a frame so I am a bit unsure how to write the syntax correctly. Below is a snippet of what the page’s code looks like to get a good idea:
First frame shown above.
Within that frame:
This is where my second frame is located. Then within that frame is my element I am after:
What I have tried
When I use the following I can find the first frame no problem:
var first_frame = window.frames["iFrameResizer0"];
first_frame.id;
Now I have tried the following and can’t get anything to return back for the second frame:
var second_frame = first_frame.frames["contentFrame"];
second_frame.id;
Also:
var second_frame = first_frame.window.frames["contentFrame"];
second_frame.id;
Also:
var second_frame = first_frame.window.document.frames["contentFrame"];
second_frame.id;
Also:
var second_frame = first_frame.document.frames["contentFrame"];
second_frame.id;
Unable to find the second frame. Does anyone know what I am missing?
2
Answers
Given that:
file:///
protocol)You can access the table element in the second frame like this:
Where "iFrameResizer0" and "contentFrame" are the IDs of the first and second frames respectively.
Here is a simple demo that demonstrates the concept. If it doesn’t work in your case there is something else going on.
index.html
one.html
two.html
Set this up as described, then click the button to see the table in the second frame logged to the console.
you may want to use
contentWindow
orcontentDocument
properties of the iframe.you may also need to wait for the iframe to load.
e.g.
demo: https://livecodes.io/?x=id/f5kd7d9jwi9&console=open