JavaScript Window frames property

The frames property in JavaScript is a property of the Window object that refers to the collection of frame or iframe elements in the current window. The frames property is an array-like object that contains references to the individual frame or iframe elements in the current window, indexed by their frame or iframe names.

Here is an example of how the frames property works:

const frame1 = document.createElement('iframe');
frame1.name = 'frame1';
frame1.src = 'https://google.com';

const frame2 = document.createElement('iframe');
frame2.name = 'frame2';
frame2.src = 'https://bing.com';

document.body.appendChild(frame1);
document.body.appendChild(frame2);

console.log(window.frames[0]);  // logs the 'frame1' element
console.log(window.frames['frame1']);  // logs the 'frame1' element

In the code example above, we use the document.createElement() method to create two new iframe elements, named frame1 and frame2. We then set the src property of each iframe element to a different website, and append the iframe elements to the body element of the document.

Next, we use the console.log() method to log the value of the window.frames[0] and window.frames['frame1'] properties to the console. Since the frame1 iframe element is the first element in the frames collection, the window.frames[0] property will refer to the frame1 element. Similarly, the window.frames['frame1'] property will also refer to the frame1 element, since the frame1 iframe element has the name 'frame1'.

The frames property is typically used to access the frame or iframe elements in the current window, allowing you to modify the properties or attributes of these elements, or to interact with the documents loaded in the frame or iframe elements.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: