Lazloman
Lazloman

Reputation: 1347

Retrieve the src of an iframe embedded within another iframe

I have a page which contains an iFrame into which I display the content of another page, which contains an iFrame; I don't control this page. I would like to get the src attr of the embedded iFrame and display that content within my iFrame. Is this possible with either jquery or javascript?

Just to be clear, here is what I have:

iframe src="somepage.html" id="mypage"

somepage.html looks like this:

iframe src="content.html"

I want to extract content.html from somepage.html and display it within "mypage" before displaying the frame. Is this possible?

Upvotes: 1

Views: 243

Answers (1)

Gijs
Gijs

Reputation: 5262

If you do not control the other page, and it is not on the same domain as you, browser security restrictions will not let you read the contents of the first iframe on the client (see eg. https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript ). This will make it impossible to read the contents of the second iframe. The only exception is that you may be able to talk from the innermost iframe to the topmost iframe by using window.top, assuming that innermost iframe is on the same domain as the topmost one -- essentially bypassing the 'middle' iframe.

Other than that, you could work around it by requesting the 'middle' frame's html contents using a proxy on the server written in a server-side language of your choice (PHP, Java, Python, Ruby, what-have-you), parsing it for the iframe and then feeding this back to your own application. Depending on what exactly you're doing, this may or may not be a viable option...

Upvotes: 1

Related Questions