logo

Silverlight: Dynamically add "favorites" bookmark link to Page

By Peter Bromberg
Printer Friendly Version
View My Articles
135 Views
    

Somebody posted this question at the Silverlight Forums and I figured I'd give it a shot. What we want is a generic "cross-browser" Add-to-Favorites script, and we want to be able to add this and a link that calls it to the underlying Page from within our Silverlight App running on the page.


 

First we need a decent javascript function that will handle this for most browsers. Here is one:

function bookmarksite(title, url)
{
   if(document.all)
      window.external.AddFavorite(url,title);
   else if(window.sidebar)
       window.sidebar.addPanel(title, url, '')
}

Our "link" would then need to look like this:

<A href="javascript:bookmarksite(document.title, location.href);">Bookmark This Page</A>

Here is all the code we would need to do this in the Silverlight Page constructor, in Page.xaml.cs:


private HtmlDocument doc;
        public Page()
        {
            InitializeComponent();
            // our new code:
            doc = HtmlPage.Document;
            // let's give the page a custom title...
            doc.GetElementsByTagName("title")[0].SetProperty("text", "ABRACADABRA!");
            // create our script function as a string
            string scr ="function bookmarksite(title, url){if(document.all)window.external.AddFavorite(url,title);else if(window.sidebar)window.sidebar.addPanel(title, url, '')}";
            // create a new script element to add to the page
            HtmlElement script = HtmlPage.Document.CreateElement("script");
            // set the type attribute
            script.SetAttribute("type", "text/javascript");
            // assign the script text
            script.SetProperty("text", scr);
            // append the script element to the document
            HtmlElement head = doc.GetElementsByTagName("head")[0];
            head.AppendChild(script);
            // create a new "a" tag
            var a = doc.CreateElement("a");
            // set the href to fire off our javascript function
            a.SetAttribute("href", "javascript:bookmarksite(document.title, location.href);");
            a.Id = "link1";
            a.SetAttribute("innerText", "Bookmark This Page!");
            a.SetStyleAttribute("font-size","20pt");
            doc.GetElementsByTagName("form")[0].AppendChild(a);
        }
The above will inject the script tag with its function into the HEAD element of the document, and add the A link into the FORM element.  You can download the complete Visual Studio 2008 Silverlight solution here.

Biography - Peter Bromberg
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Pete Tweets at peterbromberg


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Article Discussion: Silverlight: Dynamically add "favorites" bookmark link to Page
Peter Bromberg posted at Monday, September 08, 2008 7:11 PM
Original Article
 






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010