How to get all nested iframes on document?

by tressie.damore , in category: HTML & CSS , 2 months ago

How to get all nested iframes on document?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lew , 2 months ago

@tressie.damore 

To get all nested iframes on a document, you can use the following JavaScript code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Get all iframes on the document
const iframes = document.querySelectorAll('iframe');

// Function to get all nested iframes
function getAllNestedIframes(iframes) {
  let nestedIframes = [];

  iframes.forEach(iframe => {
    nestedIframes.push(iframe);

    // Get all iframes within the current iframe
    const nested = getAllNestedIframes(iframe.contentDocument.querySelectorAll('iframe'));
    nestedIframes = nestedIframes.concat(nested);
  });

  return nestedIframes;
}

// Call the function to get all nested iframes
const allNestedIframes = getAllNestedIframes(iframes);

// Log the nested iframes
console.log(allNestedIframes);


This code will recursively traverse all iframes and their children to get all nested iframes on the document. Once you have the nested iframes, you can perform any desired operations on them.