views:

156

answers:

3

Let's say I get the source code of some page (e.g. http://example.com). I now want to write this source code to a WebBrowser, using something like:

myWebBrowser.Navigate("about:blank");
myWebBrowser.Document.Write(sourceCode);

Now, let's pretend that on the homepage of Example.com, there's a relative URL such as:

<img src="/logo.gif" />

The WebBrowser will attempt to load it from about:blank/logo.gif. I want to tell the WebBrowser that the "current address" is http://example.com so that it uses http://example.com/logo.gif instead.

Writing directly to the Url property of the WebBrowser will cause a Navigate(), which will get rid of any text I wrote.

I am looking for a solution that works for other elements as well such as stylesheets, javascript (e.g. <script language="text/javascript" src="myscript.js">), links, etc., not just images.

Is this possible?

+4  A: 

You could try and insert a <base> element in the head: http://www.w3.org/TR/html4/struct/links.html#edef-BASE

How to insert the tag is dependent upon the language you're using but you should aim to get the base tag directly after the <head> so that the resulting source reads:

<head><base href="http://example.com"/&gt;

Of course, if there is already a <base> element in the document, you should remove that.

Roland Bouman
+3  A: 

If you try to change address bar URL, browser will navigate through that, and there's nothing you can do about that.

But, in your HTML, its possible to "fake" source URL for your resources, by using <BASE> tag.

I think this is closest you can go.

Rubens Farias
+3  A: 

You can inject a <BASE> element in your code and state that the base is http://example.com

naivists