Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an iframe DOM element given its URL

I'm using the w3c Resource Timing API. window.performance.getEntriesByType("resource"); This gives a list of resources including iframes. However, to get hold of the resources of the embedded iframes, I need to get hold of the iframe's performance object which requires me to have the reference to its' DOM node/element.

        var myiframe2 = $("#myiframe2");
        var myiframeContentWindow = myiframe2[0].contentWindow;
        var iframeContentPerf = myiframeContentWindow.performance;

The question is, how to get hold of the iframe's node reference if all I know is the URL of the iframe, which is all I know(I will not know the iframe's id or name unlike the code sample above).

Other than iterating through the document's elements and then filtering iframes and comparing the URL of the iframe with what the Resource Timing API returned, is there a simple way to get hold of the iframe's DOM node (given the iframe's URL) ?

like image 819
anjanb Avatar asked Oct 01 '15 07:10

anjanb


People also ask

How do I get body element of iframe?

document. body. innerHTML: It returns the HTML content of iframe body.

Can you access Dom in iframe?

This property can be used in the host window to access the Document object that belongs to a frame or iframe element. Note: Because of security reasons, the contents of a document can be accessed from another document only if the two documents are located in the same domain.

Is an iframe a DOM element?

The IFrame Object property in HTML DOM is used to create and access the <iframe> element within the object. An inline frame is used for embedding another document within the current HTML document.

How do I get cross domain iframe content?

To access cross-domain iframe, the best approach is to use Javascript's postMessage() method. This method provides a way to securely pass messages across domains.


1 Answers

If the URL of the iframe has not changed, you can try to get it directly with CSS selectors using the exact URL:

document.querySelector('iframe[src="http://example.org"]');

If the URL will change but you have a unique part per iframe (which can identify a specific iframe):

document.querySelector('iframe[src*="example.org/some/url"]');
like image 199
avetisk Avatar answered Oct 08 '22 05:10

avetisk