Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the document object of a frame with JavaScript

I'm testing on this page, and I'm not sure what I'm missing.

// Two frames on the page
> document.getElementsByTagName("frame").length
2

// Same domain, so no security restrictions
> document.getElementsByTagName("frame")[0].src
"http://www.quackit.com/html/templates/frames/menu_1.html"
> window.location.href
"http://www.quackit.com/html/templates/frames/frames_example_1.html"

// Can't access the document
> document.getElementsByTagName("frame")[0].document
undefined

It seems like this should work, so what's the problem? It needs to work in IE8, but I'm also testing in Chrome (newest stable).

like image 566
Brigand Avatar asked Feb 18 '13 20:02

Brigand


People also ask

How do you access an element inside a frame?

Once we move the driver focus inside the frame, we can access the elements inside the frame by the xpath locator with the help of the driver. findElement(By. xpath(<xpath value>)) method.

Is document object in JavaScript?

A Document object represents the HTML document that is displayed in that window. The Document object has various properties that refer to other objects which allow access to and modification of document content. The way a document content is accessed and modified is called the Document Object Model, or DOM.

What is frame object in JavaScript?

Frame object represents an HTML frame which defines one particular window(frame) within a frameset. It defines the set of frame that make up the browser window. It is a property of the window object. Syntax:<frame>

Which attributes display documents in the frame?

The src attribute specifies the initial document the frame will contain. and cause the user agent to load each file into a separate view. The contents of a frame must not be in the same document as the frame's definition.


2 Answers

The all-around way to getting a frame's contents is with something like this:

var theFrame = document.getElementsByTagName("frame")[0];
var theFrameDocument = theFrame.contentDocument || theFrame.contentWindow.document;
var button = theFrameDocument.getElementById("mybutton");

However, it is possible to get a <frame>'s document by using its name, like:

window.frames["frame_name"].document

if the HTML were:

<frame name="frame_name">...</frame>
like image 93
Ian Avatar answered Sep 21 '22 20:09

Ian


You could use

parent.frame.location.href = ...

Where frame is the name/id of the frame you d like to change.

Greets Marc

like image 26
mooonli Avatar answered Sep 22 '22 20:09

mooonli