Search code examples
javascripthtmliframe

Select all iframes and check them with javascript


My code is here:-

<iframe src="http://www.website.com/abc.ext" width="600px" height="400px"></iframe><br />
<iframe src="http://www.anotherwebsite.com/link.py" width="600px" height="400px"></iframe>

Now, I want to select both iframes with javascript and check their src. I want to allow only www.website.com for iframe. I want to build a javascript code what will show only the iframes containing www.website.com and others will be hidden.

Please let me know is it possible?


Solution

  • var frames = document.getElementsByTagName('iframe');
    for (var i in frames)
        if (!frames[i].src.match(/^http:\/\/www\.website\.com/))
            document.body.removeChild(frames[i]);
    

    Something like this. You can replace the condition and do what you want as per your need.