Sign In

Navigation

On This Page

Archive

<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

Categories

Blogroll

Contact

Send mail to the author(s) Email Me
MCPD
MCTS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way


Copyright ©  2012
 Creative Commons License
This work by Jeff Klawiter is, unless explicitly stated in the article,  available under the Creative Commons Attribution 3.0 United States License.

Pick a theme:
# Saturday, April 24, 2010
by Jeff Klawiter - Saturday, April 24, 2010 3:50:26 PM (Central Standard Time, UTC-06:00)

There is a “bug” with Silverlight in Safari when trying to do window navigation. Both HtmlPage.PopupWindow and HtmlPage.Window.Navigate will not work on Safari due to plugin limitations. This makes doing something like using AddThis’ API rather hard to do cross browser. Chris Idzerda over at Vertigo had posted a partial solution a while back but it had the drawback of needing to always have more html along with your silverlight app. I decided to take his solution and make it a bit more automatic.

Chris’ solution involved having certain HTML exist on the page already that Silverlight can call. While this works, I hate having to dictate more and more things that need to be on the page. Below is his solution

public static void  OpenBrowser(string url)
{
  if(HtmlPage.BrowserInformation.UserAgent.Contains("Safari"))
  {
    HtmlElement anchor= HtmlPage.Document.GetElementById("externalAnchor");
    anchor.SetProperty("href", url);
    HtmlElement button= HtmlPage.Document.GetElementById("externalButton");
    button.Invoke("click", null);
  }
 else
    HtmlPage.Window.Navigate(new Uri(url, UriKind.RelativeOrAbsolute), "_blank");
}
<a  id="externalAnchor" style="display:none;"></a>
<input id="externalButton" type="button" onclick="window.open(document.getElementById('externalAnchor').href)" style="display:none;" />

My idea was to take his html and dynamically create it via Silverlight’s Html bridge. (Note this will only work if the xap is on the same domain or “enablehtmlaccess” is set to true for the plugin). To do this is fairly straight forward. Below is my code (with the exception handling simplified)

if (HtmlPage.BrowserInformation.UserAgent.ToLower().Contains("safari"))
{
    try
    {
        if (HtmlPage.IsEnabled)
        {
            var anchor = HtmlPage.Document.CreateElement("a");
            anchor.Id = "externalAnchor";
            anchor.SetStyleAttribute("display", "none");
            HtmlPage.Document.Body.AppendChild(anchor);

            var button = HtmlPage.Document.CreateElement("input");
            button.Id = "externalButton";
            button.SetAttribute("type", "button");
            button.SetAttribute("onclick", "window.open(document.getElementById('externalAnchor').href)");
            button.SetStyleAttribute("display", "none");
            HtmlPage.Document.Body.AppendChild(button);
        }
    }
    catch (Exception e)
    {
        OnError(e.Message);
    }
}

So starting off only if we are in Safari do we even need to run the code. I opted to run this code when my AddThis component is first loaded. If we have access to the HtmlPage we new up anchor and button elements. This generates the exact same HTML that Chris’ did in his example. After this his function from above just works :). It’s a fairly straightforward fix. I hope that soon MS will be able to get SL to support popup/navigation in Safari. I’m sure it has something to do with their plugin api.

Comments [0] #       |  kick it on DotNetKicks.com Shout it
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview