0

I have an HTML document with an iFrame, which i am loading in a webbrowser window. My objective is once that iFrame document is loaded, capture the document contents and search for a particular line.

I am not been able to do it so far, any suggestions ?

HTML CODE

<html>
  <body>
    <iframe id="monitor" src="http://monitor.baseline.com"></iframe>
  </body>
</html>
captainsac
  • 2,484
  • 3
  • 27
  • 48
Telson Alva
  • 842
  • 6
  • 22
  • 37

2 Answers2

1

You can add a JavaScript function to the OnLoad event of the iFrame:

document.getElementById('monitor').onload = function() { }

And then follow these solutions to parse your document. But be aware about cross-site-scripting restrictions. If the src of your iFrame is a totally different domain, it probably won't work.

Community
  • 1
  • 1
LInsoDeTeh
  • 150
  • 2
  • i want to read the contents once iframe is loaded. trying some like the below but doesnt seem to work .. var Frame = document.getElementById('monitor'); alert(Frame.innterHTML); alert(Frame.outerHTML); window.external.iFrameCheck(Frame.outerHTML); – Telson Alva Jun 10 '15 at 07:08
  • I think you can't use innerHTML for an iFrame, as it's not the inner html, but the content of the src. Check the link I posted, there are a few solutions how to access iFrame content. – LInsoDeTeh Jun 10 '15 at 07:28
0

i got it by doing the below

var javascript = @"

                        var Frame = document.getElementById('monitor');
                        window.external.iFrameCheck(Frame.contentWindow.document);
                        ";

            var doc = (IHTMLDocument2)webBrowser1.Document;

            //Once we have the document, execute our JavaScript in it
            doc.parentWindow.execScript(javascript);

this would then call the below c# code

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        [ComVisible(true)]
        public class ComVisibleObjectForScripting
        {
            public void iFrameCheck(HTMLDocument FrameDoc)
            {
                //Do what you want here with your FrameDoc
                // the innerHTML will give you the content of the iframe
            }
        }
Telson Alva
  • 842
  • 6
  • 22
  • 37