开发者

How do I search an iframe for a specific image or grab the source code

My m开发者_C百科ain goal is to search an iframe for a specific image. I know what the image will be (abc_clicked.gif) but am not sure how I can access the iframe to either:

1) search the iframe for the image itself 2) grab the source code in which I will manually search myself for the image

I am looking trying to accomplish this with javascript, as I don't see how PHP could help me at all in this case.

Anyone have any ideas???? I'm lost....


If the iFrame is hosted on the same domain, you can access the DOM the same as you would for the main page using contentDocument.

For example:

var iframeElement = document.getElementById('myiframe');
var imageElement = iframeElement.contentDocument.getElementById('myImage');


(assuming you're working in a Web page and looking for a JavaScript solution)

If the iframed page is in a different domain, there's not much you can do.

If it's in the same domain, here is a cross-browser way to access it's content:

var doc=ifr.contentWindow||ifr.contentDocument;
if (doc.document) doc=doc.document;

You can then search your iframe:

var imgs = doc.getElementsByTagName("img");
// etc.

Your second option is also valid (but might be more complicated), use ajax to retrieve and parse the page source.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜