BUG: document.write from Automation Client May Change URL to about:blank (272760)



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 5.01
  • Microsoft Internet Explorer (Programming) 5.01 SP1
  • Microsoft Internet Explorer (Programming) 5.5
  • Microsoft Internet Explorer (Programming) 6.0

This article was previously published under Q272760

SYMPTOMS

When Internet Explorer is automated from an application that replaces the HTML document using the document.write method, and the HTML code contains an IFrame element, the IFrame may not display the intended page. Also, the URL in the address bar may change to about:blank.

RESOLUTION

The problem does not occur when the script within the page rewrites the document. You can insert the script function that rewrites the page into the document, and then call the script function. See the "More Information" section for an example.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. In Microsoft Visual Basic, create a Standard EXE project.
  2. Add references to the Microsoft HTML Object Library and the Microsoft Internet Controls.
  3. In the Load event of Form1, paste the following code to start Internet Explorer and navigate to a test page:
    Dim WithEvents ie As InternetExplorer
    Dim doc As IHTMLDocument
    
    Private Sub Form_Load()
        Set ie = New InternetExplorer
        ie.Visible = True
        ie.navigate "http://myserver/test.htm"
    End Sub
    					
  4. To replace the HTML code on the current page, add a button to the form, and then add the following code to the Click event:
    Private Sub Command1_Click()
        Dim strHTML As String
        
            Set doc = ie.document
        
            strHTML = "<html><body><DIV>hi<br>" & _
                      "<iframe src=testiframe.htm" & _
                      " width=200 height=70>" & _ 
                      "</iframe><DIV></body></html>" 
            
            doc.open
            doc.write strHTML
            doc.Close
    End Sub
    					
  5. Build the application.
  6. Create a new file named Test.htm and paste the following code:
    <html>
    <body>
    hi
    </body>
    </html>
    					
  7. Create a new file named TestIFrame.htm and paste the following code:
    <html>
    <body>
    This is my iframe.
    </body>
    </html>
    					
  8. Execute the Visual Basic project and then click the button that you added. The address bar in Internet Explorer changes to about:blank, and the IFrame within it does not show the TestIFrame.htm file. The source of the IFrame is
    <HTML>blanktestiframe.htm</HTML>
    					
To work around this problem, insert script code into the HTML code and call it from the Automation client. You can use the following code in place of the Command1_Click() procedure:
Private Sub Command1_Click()
    Dim strScript As String

    Set doc = ie.document
    
    strScript = "<span style=" & Chr(34) & "display:none" & Chr$(34) & _
                 ">h</span>" & _
                "<script defer=true language=" & Chr(34) & _
                "javascript" & Chr(34) & ">" & _
                "function insertIFrame()" & "{" & _
                "    var alltext = " & Chr(34) & _
                "<html><body>Written from VB" & _
                "Automation<br>" & _
                "<iframe src=\" & Chr(34) & "testiframe.htm\" & Chr(34) & _
                " width=200 height=70>" & _
                "</iframe></body></html>" & Chr(34) & ";" & _
                "    document.open();" & _
                "    document.write(alltext);" & _
                "    document.close();" & "}" & _
                "</script>"

    doc.documentElement.insertAdjacentHTML "beforeEnd", strScript
    doc.parentWindow.execScript "insertIFrame()", "jscript"


End Sub
				

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

185128 HOWTO: Insert Event Handler Into Web Page from WebBrowser App

For more information, see the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:5/12/2003
Keywords:kbAutomation kbBug kbDHTML KB272760