user1428798
user1428798

Reputation: 1546

webbrowser windows phone + tel and mailto tag don't work

I'm using a webbrowder to render a html string.

private void webBrowserHTML_Loaded(object sender, RoutedEventArgs e)
        {
            WebBrowser web = sender as WebBrowser;
            string description = web.DataContext.ToString();
            web.NavigateToString(description);
        }

in this html, i have tags tel and mailto:

<a href="mailto:[email protected]"> Envoyer un email </a> 
<a href="tel:+33102030405"> Appeler la société xxx </a>

My problem when i click the number, i don't call it and when i clik the mail is not open my outlook!!

Any solution please?

Upvotes: 1

Views: 1609

Answers (3)

Ranjith Varma
Ranjith Varma

Reputation: 473

<onclick="window.location.href = 'tel:1231231234';">

instead of

<a href="tel:+33102030405">

Thanks

Upvotes: 1

user2025306
user2025306

Reputation: 11

Great fix, the above solutions works much better,

Upvotes: 0

Olivier Payen
Olivier Payen

Reputation: 15268

Unfortunately, mailto: and tel: are not supported by the WebBrowser control on Windows Phone.

What you can do is inject Javascript in the HTML that will enumerate all a tags and wire up an onclick event. That event will call window.external.Notify which will in turn raise the ScriptNotify event of the WebBrowser, with the URL as a parameter.

It is a little complicated but I think it's the only option for dealing with these mailto and tel protocols on Windows Phone.

Here is the code:

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        browser.IsScriptEnabled = true;
        browser.ScriptNotify += browser_ScriptNotify;
        browser.Loaded += browser_Loaded;
    }

    void browser_Loaded(object sender, RoutedEventArgs e)
    {
        // Sample HTML code
        string html = @"<html><head></head><body><a href='mailto:[email protected]'>Envoyer un email</a><a href='tel:+3301010101'>Appeler</a></body></html>";

        // Script that will call raise the ScriptNotify via window.external.Notify
        string notifyJS = @"<script type='text/javascript' language='javascript'>
                                window.onload = function() {
                                    var links = document.getElementsByTagName('a');
                                    for(var i=0;i<links.length;i++) {
                                        links[i].onclick = function() {
                                            window.external.Notify(this.href);
                                        }
                                    }
                                }
                            </script>";

        // Inject the Javascript into the head section of the HTML document
        html = html.Replace("<head>", string.Format("<head>{0}{1}", Environment.NewLine, notifyJS));

        browser.NavigateToString(html);
    }

    void browser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Value))
        {
            string href = e.Value.ToLower();
            if (href.StartsWith("mailto:"))
            {
                EmailComposeTask email =  new EmailComposeTask();
                email.To = href.Replace("mailto:", string.Empty);
                email.Show();
            }
            else if (href.StartsWith("tel:"))
            {
                PhoneCallTask call = new PhoneCallTask();
                call.PhoneNumber = href.Replace("tel:", string.Empty);
                call.Show();
            }
        }
    }

Upvotes: 1

Related Questions