Link to home
Start Free TrialLog in
Avatar of jodyglidden
jodyglidden

asked on

Changing url in webbrowser control

Hi,
In order to avoid loading images for "some" urls that I go to the the webbrowser control, I've tried the following code.  The problem is that none of the relative links work in the webbrowser when the page loads.  I think that's because the locationurl is still about:blank.  I try below using the putproperty method of the webbrowser control to set the locationurl property manually however it doesn't seem to work.  Any ideas?


Private Sub Form_Load()
'First time round, make sure
'there is a document in the webbrowser

WebBrowser1.Navigate ("about:blank")

'Navigate to a page without images like this:
NoImageNavigate ("http://www.google.com")

End Sub

Private Sub NoImageNavigate(myURL As String)
Dim myhtml As String
  myhtml = Inet1.OpenURL(myURL)
  'Writing this string to the webbrowser will be enough
  'to lose the relative references to images.
  'But the following 'replaces' will kill
  'any absolute references also. The browser
  'will ignore the 'bad syntax' that this creates
  myhtml = Replace(myhtml, "<img ", "<p ")
  myhtml = Replace(myhtml, "<IMG ", "<p ")
  myhtml = Replace(myhtml, "< img ", "<p ")
  myhtml = Replace(myhtml, "< IMG ", "<p ")

 
  WebBrowser1.Document, "LocationURL", myURL
 
  With WebBrowser1.Document
    .open
    .write myhtml
    .Close
  End With
End Sub
Avatar of Erick37
Erick37
Flag of United States of America image

You can change the image source in this way:

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
   
    Dim i As Long

    For i = 0 To WebBrowser1.Document.images.length - 1
   
        WebBrowser1.Document.images.Item(i).src = "file://c:\repl.jpg" '<< Point to Replacement image
   
    Next
   
End Sub

Hope it helps!
And another way:

Dim iTag As IHTMLElement 'Reference the Microsoft HTML Object Library

For Each iTag In WebBrowser1.Document.All.tags("img")
    iTag.src = "c:\repl.jpg"
Next
Avatar of jodyglidden
jodyglidden

ASKER

Hi,
Actually, what I was trying to do is download without downloading the images.  That's why I used the inet control to download the html, then took out the old not working image tags.  My code works but then none of the links work.
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good thinking.  I hadn't thought of that approach.  How would we retain the necessary cookies though so that links would work?  By the way, the code for some reason seems to be loading images anyway.  Not sure why that would be.
Hi jodyglidden,

I've been following your quest to control what the Webbrowser downloads.  Unfortunately there does not seem to be a good answer for VB6.

I downloaded the Emorcillo project and got the same error you reported in your other question.  What little documentation there was stated the code was tested with Windows Me (not NT, 2000, XP).

So far the only viable solution seems to be wrapping the Webbrowser control inside a C++ ATL project in order to catch the request for DISPID_AMBIENT_DLCONTROL.  But this is beyond me, and the ones capable of doing so probably aren't using VB anyway!

I'll keep my eyes open for you.

Best of luck.

Some reading:
See the section "The Good, the Bad, and the Heart-Thumping News"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui10232001.asp