search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

Silverlight: Dynamically add "favorites" bookmark link to Page


By Peter Bromberg
Printer Friendly Version
View My Articles
5 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.
Please post questions at forums, not via email!

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