Loading HTML into a WEB Browser Control from Variable

topic: 

NOTE

The Web Browser control works much better when loading HTML from a file.

This is sample code. Add error handling and adjust to your requirements as necessary.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

lcHtml = [<html><body>Hello World!</body></html>]
lnLoadTimeout = 3       && seconds
WITH Thisform.oWeb.OBJECT
	.Navigate("about:blank")
	* Wait for load completion
	lnStartSeconds = SECONDS()
	DO WHILE .ReadyState <> 4 ;
			AND (SECONDS()-lnStartSeconds <= lnLoadTimeout )
		DOEVENTS
	ENDDO
	* Load HTML into Web Browser Control
	.Document.Open()
	.Document.Write(lcHtml)
	.Document.Close()
	* Wait for load completion
	lnStartSeconds = SECONDS()
	DO WHILE .ReadyState <> 4 ;
			AND (SECONDS()-lnStartSeconds <= lnLoadTimeout )
		DOEVENTS
	ENDDO
ENDWITH

See Also Loading XML into a WEB Browser Control from Variable

Comments

Thanks Sergey

Thanks Sergey,

This was extremely helpful. I just found myself in a situation when the WebBrowser could not finish loading a simple HTML file. (On the background I have another ActiveX component running that was probably bringing the issue). Using this aproach everything runs perfectly.
<b>Thanks again!</b>

Just for the records, in case this may help someone else.
My final solution was to use SYS(2333,1), to acrtvate the ActiveX dual interface support.

Regards

Cesar

Sergey, the above code works perfectly. I was searching for a solution for a long time. i modifed the code to have the lcHtml be a filetostr( "c:\temp\test.html") and test.html changes as i need it to. I have a question though. When I go to load a new HTML file the code stops at the
1
 .Navigate("about:blank") 
. I implemented the sys( 2333,1) but that did not seem to help. I have to C-A-D/Task Manager to terminate foxpro. Any help would be appreciated.

Thanks, Josh.

Hi Josh,

Can you load HTML and than "about:blank" in IE w/o problem?

BTW, you can load HTML from a file directly
1
2
3
lcHtmlFileName = "c:\temp\test.html"
.Navigate2("file://" + lcHtmlFileName)


Why is it necessary to always navigate to a the "about:blank" page before loading the actual page that I want to show?

It's not necessary. I prefer to do it this way.

I'm finding that if I *do not* call the blank page, the browser control does not refresh to the new page at all.

I guess you also remove code that waits for load to complete. Anyway, The Web Browser control works much better when loading HTML from a file.

Hi Sergey.
I have problem with this Active X, send a Ole error code
and then when I run the example in this line: .Document.Open()
occurr other error. I think that the second error is a consequence of the first error.
You have any idea about that ....

Mario

Hi Sergey, something seems to be wrong with the sample code on this post. It looks like maybe some of it was accidentally removed (?).

Fixed