HTMLViewer mailto: and http://

I’m using HTMLViewer to display some email and web URL’s (from contacts) as hyperlinks but the results seem inconsistent.

Firstly mailto: doesn’t seem to link at all - ie it doesn’t open a new email in Mail.
Secondly http:// is inconsistent. Some sites work with http:// (e.g. Apple), whilst others only work with https:// (eg xojo).

Here’s some simple HTML to illustrate:

<!DOCTYPE html>
 <a href="">johnny</a>
 <a href="">xojo</a>
 <a href="">xojo</a>
 <a href="">xojo</a>
 <a href="">apple</a>

Is this a “feature” ?

For mailto URIs you will need to catch them in the CancelLoad event and handle the new email command.
Such as this:

If url.beginswith("mailto:") then
return True
End if

As for http:// working in some cases it must be caused by the redirection method used by the server. As to why works and does not, I cannot explain it.

If you add the Error event to HTMLViewer you can see that trying to go to the RuntimeException is:

The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

There is no exception if the url is

There is no error and nothing happens if the href is only

Unfortunately the OP didn’t indicate what platform he’s using. Here’s what I do in CancelLoad:

// Called when the content of the HTMLViewer is about to be replaced. If this is due to loading
// an html string into the control, then it seems the URL will be "about:blank". If it happens
// due to the user clicking on a link in the HTMLViewer, we want to show that one. Any links in
// the control will either have no "target" attribute, or it will  have been be changed to "_self".
// This will force all clicked links to trigger this event, which then uses GotoURL() to display
// the linked page in the user's browser, rather than here.
// Under Win7 the page loaded from code goes via a temporary file. When an external link is
// clicked, the URL has the normal form, but also another event occurs with the "about:blank" URL.
// For macOS, mailto: is handled by an AppleEvent triggered by the GotoURL. For Win/Lin, it is
// handled otherwise, so nothing should be done here.

if  (URL="about:blank" or URL.BeginsWith("file://")=True or URL.BeginsWith("c:\Users\")=True)  then Return False

#if  (TargetMacOS=False)  then
  if  (URL.BeginsWith("mailto:")=True)  then Return True           // The HTMLViewer should ignore this request

system.GotoURL (URL)

Return True

OK I deduce he is on macOS. Then what I have posted will work.

The thing to do is to implement all the event handlers for HTMLViewer and see which ones fire under which circumstances. It’s what I did and that gave me the info I needed (along with reading the docs).

See my other post about what to do with mailto:

Thanks all. Yes it was OSX.

I didn’t understand the cancel load event before in this context as mailTo doesn’t load into the Viewer.

However, the answers work fine.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.