Getting correct URL for HTMLViewer

I have a mini-browser – a window with a URL text field and an HTMLViewer control. When the DocumentComplete event fires, I display the url parameter in the URL field. This usually works, but there are sites that use JavaScript to access ad servers once the main URL is loaded. My app can’t distinguish the JS-generated URLs from legitimate ones, and so the URL field ends up displaying not the URL of the web page, but the URL of an ad server.

I’ve tried trapping for illegitimate URLs (like about:blank) that are generated and using HTMLViewer1.cancel (or the CancelLoad event), but that just blocks that particular access, the JS keeps sending new URLs to “load”.

Real browsers don’t display these false links in their URL fields, so they must be able to distinguish between “real” URLs and those that are just fetching ad information.

Is there some way to do this in a Xojo HTMLViewer?

I am on Windows. ( In case you are a Mac fellow ). I hit a similar issue - all kinds of garbage and there is simply no way to predict what, or code around it. The solution was to add in a MS Webbrowser, then, in the DocumentComplete event, to use the webbrowser.LocationUrl which gives the correct url.

Thanks for your reply, but I am a Mac fellow ( :slight_smile: ). Well, in any case that gives me hope this will be possible. Anyone know of a cross-platform solution?

Here is what you do to get the current URL.

Code in a button. To obtain the URL after the last part of the page has loaded, reset a timer in each Document Complete, so after the last one has elapsed, the timer fires :

Sub Action() HTMLViewer1.ExecuteJavaScript("document.title='zurl '+document.URL;") End Sub

You get the URL in the Titlechanged event :

Sub TitleChanged(newTitle as String) if instr(newTitle,"zurl")>0 then dim myURL() as string = split(newTitle, "zurl") msgbox myURL(1) end if End Sub

Note the ‘zurl’ label I used to identify the title in which the URL is sent back from the JavaScript. You can also use it to distinguish between the JavaScript result and the regular title.

I used the cnn.com site which is a good example of a site that takes you to another location.

cnn.com in fact ends up as edition.cnn.com after going through three intersticial pages.

MBS Plugin has functions to query current URL.
Mac? Win? Linux?
Native or Webkit renderer?

I’m on OS X. Thanks for the tip about JS, that was very helpful. It works, but is a bit cumbersome because it relies on generating an event that the HTMLViewer responds to. I see that MacOSLib has a class extension for HTMLViewer with a method EvalueateJavaScript. With it installed, this call does the trick

dim s as string = HTMLViewer1.EvaluateJavaScript(“window.location.href” )

[quote=184911:@Jonathan Ashwell]I’m on OS X. Thanks for the tip about JS, that was very helpful. It works, but is a bit cumbersome because it relies on generating an event that the HTMLViewer responds to. I see that MacOSLib has a class extension for HTMLViewer with a method EvalueateJavaScript. With it installed, this call does the trick

dim s as string = HTMLViewer1.EvaluateJavaScript(“window.location.href” )[/quote]

I posted the JS for cross platform usage. Indeed on Mac there are specific declares that may be more convenient.

Hi everyone!

I had the same problem Jonathan Ashwell, so I tried Michel Bujardet’s solution. Everything is fine except when I trie to put the result of the “msgbox” into the url textfield. I tried this code:

HTMLViewer1.ExecuteJavaScript("document.title='zurl '+document.URL;") if instr(newTitle,"zurl")>0 then dim myURL() as string = split(newTitle, "zurl") TextField1.Text = myURL end if

When I try to run the application, it tell me that the line TextFiel1.Text = myUrl “Type mismatch. Expected String, but got String()”.

TextField1 is the name of my field where the url should be written.

Since I am new with Xojo, I thought that it could work.

So how do we display the url return in the msgbox of Michel Bujardet’s solution into the textfield?
If myURL is not a string, what is it?

Tank you to take your time.

Francis

myURL() is an array of strings. The original code above says msgbox myURL(1). You’re missing (1) at the end of myURL. myURL(1) means the second item in the array called myURL. Numbering starts with 0.

wow!
Tank you Scott, it solve my question.

Tanks for the fast answer.

Continue your great job gys!

Francis