webBrowser control embedded in page doesn't get body element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kenia
    New Member
    • Dec 2006
    • 7

    webBrowser control embedded in page doesn't get body element

    Hi,
    I'm trying to get the body element of a page using a webBrowser control. Right now I'm testing in javascript to make sure i can access the body through the webBrowser control, but soon I'll create an activeX control and use the control from there.
    My code is:

    var wb = document.getEle mentById("wbSav e");
    wb.Navigate(win dow.location);
    var body= wb.Document.bod y;

    The point is I don't know why the body is always null. If I analize the Document children I see it contains the "head" element and its contents but I would expect it to contain the body as another child too, and however it doesn't. I've searched online in several forums and everybody seems to access the body element this way, I just can't. I'm completely clueless.
    So, please if anyone can give me any help on this, it will be appreciated.

    Thanks in advance

    Kenia
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Is this a .NET control?

    Comment

    • kenia
      New Member
      • Dec 2006
      • 7

      #3
      Hi and thanks for replying.
      No, it's not a .Net control, it's the ActiveX control used by InternetExplore r.
      Below is the code of my page

      [HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head id = "head">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Salvar reporte</title>
      <script>
      function window_onload()
      {
      if(window.opene r != null)
      {
      var content = window.opener.d ocument.getElem entById("messag es");
      var pageBody = document.getEle mentById("pageB ody") ;
      var div = document.create Element("div");
      div.innerHTML = content.innerHT ML;

      //Add styles
      var openerStyle = window.opener.d ocument.getElem entById("style" );
      var style = document.create Element("style" );
      style.setAttrib ute("type", "text/css");

      //IE compatible
      if(style.styleS heet)
      {
      style.styleShee t.cssText = openerStyle.sty leSheet.cssText ;
      } else // w3c (Firefox)
      {
      var text = openerStyle.chi ldNodes[0].nodeValue;
      style.appendChi ld(document.cre ateTextNode(tex t));
      }

      var head = document.getEle mentById("head" );
      head.appendChil d(style);

      pageBody.append Child(div);
      }
      }

      function save()
      {
      var wb = document.getEle mentById("wbSav e");
      wb.Navigate(win dow.location);
      var doc = wb.Document;
      alert(doc.body) ;
      //wb.ExecWB(4,1);
      }
      </script>
      </head>

      <body onload="window_ onload()" id="pageBody">
      <object id="wbSave" width=0 height=0 classid="clsid: 8856F961-340A-11D0-A96B-00C04FD705A2"></object>
      <p id="par">this is a test</p>
      <input type="button" onclick="save() " value="Save" />
      </body>
      </html>
      [/HTML]
      I'm always getting a null value, and I read on MSDN and it should work, so I'm completely clueless, don't know what else to try....
      If you have any idea, please share it with me.

      Thanks a lot

      Kenia
      Last edited by acoder; Mar 24 '08, 07:36 PM. Reason: Added code tags

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        Originally posted by kenia
        Hi and thanks for replying.
        No, it's not a .Net control, it's the ActiveX control used by InternetExplore r.
        Below is the code of my page

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head id = "head">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Salvar reporte</title>
        <script>
        function window_onload()
        {
        if(window.opene r != null)
        {
        var content = window.opener.d ocument.getElem entById("messag es");
        var pageBody = document.getEle mentById("pageB ody") ;
        var div = document.create Element("div");
        div.innerHTML = content.innerHT ML;

        //Add styles
        var openerStyle = window.opener.d ocument.getElem entById("style" );
        var style = document.create Element("style" );
        style.setAttrib ute("type", "text/css");

        //IE compatible
        if(style.styleS heet)
        {
        style.styleShee t.cssText = openerStyle.sty leSheet.cssText ;
        } else // w3c (Firefox)
        {
        var text = openerStyle.chi ldNodes[0].nodeValue;
        style.appendChi ld(document.cre ateTextNode(tex t));
        }

        var head = document.getEle mentById("head" );
        head.appendChil d(style);

        pageBody.append Child(div);
        }
        }

        function save()
        {
        var wb = document.getEle mentById("wbSav e");
        wb.Navigate(win dow.location);
        var doc = wb.Document;
        alert(doc.body) ;
        //wb.ExecWB(4,1);
        }
        </script>
        </head>

        <body onload="window_ onload()" id="pageBody">
        <object id="wbSave" width=0 height=0 classid="clsid: 8856F961-340A-11D0-A96B-00C04FD705A2"></object>
        <p id="par">this is a test</p>
        <input type="button" onclick="save() " value="Save" />
        </body>
        </html>

        I'm always getting a null value, and I read on MSDN and it should work, so I'm completely clueless, don't know what else to try....
        If you have any idea, please share it with me.

        Thanks a lot

        Kenia
        One potential issue I see is that you are calling the JS function before the body tag is fully loaded. You might try moving the function call to the end of the page after the closing body tag. I do not know about ActiveX, but I know in JS you will usually have problems if you run a function on an element that has not yet been loaded in the page.

        Comment

        • kenia
          New Member
          • Dec 2006
          • 7

          #5
          Originally posted by pronerd
          One potential issue I see is that you are calling the JS function before the body tag is fully loaded. You might try moving the function call to the end of the page after the closing body tag. I do not know about ActiveX, but I know in JS you will usually have problems if you run a function on an element that has not yet been loaded in the page.
          The function that's called when the body onload event fires is not the one that has the problem. Actually the save() function is the one that uses the ActiveX, and it's called when the save button is pushed, by that time the body is completely loaded.
          If you see something else... please let me know.

          Thanks

          Kenia

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by kenia
            I'm always getting a null value, and I read on MSDN and it should work,
            Can you provide the link for that? If there's an example, have you tried the example only?

            Comment

            Working...