Posted 30 September 2007 Tweet
In the versions of Microsoft Silverlight that have been released to date, the managed HTML DOM interfaces have stayed pretty basic. In the process of building prototype applications on top of these bits, I've therefore developed some really simple methods that've proved useful to me, and cut down on typing repetitive code. It's rainy in Seattle today, so I figured it would be a great time to post about a few of these methods. The methods and properties are all simple, and self explanatory...I should point out that the last method (HtmlElement.AppendChildren) does require C# 3.0 to compile as it is an extension method. And also, a friendly reminder to include a using statement for System.Windows.Browser. Update: Download of HtmlExtensions.cs.txt also available. /// <summary> /// A set of C# 3.0 extension methods *as well* as a general /// HTML DOM bridge helper & utility library /// </summary> public static class HtmlExtensions { /// <summary> /// Retrieve the first HtmlElement in the document that matches /// the tag name /// </summary> /// <param name="tagName">HTML tag</param> /// <returns>The first HtmlElement of tagName on the page</returns> public static HtmlElement GetSingleElementByTagName(string tagName) { HtmlElementCollection hh = HtmlPage.Document.GetElementsByTagName(tagName); if (hh.Count > 0) { return hh[0]; } throw new InvalidOperationException( String.Format(@"There were no ""{0}"" HTML tags found on the page.", tagName)); } /// <summary> /// Property representing the <head /> element on /// the page. /// </summary> /// <remarks>Although a static property, this get is actually /// a method call</remarks> public static HtmlElement HeadElement { get { return GetSingleElementByTagName("head"); } } /// <summary> /// Property representing the <body /> element on /// the page. /// </summary> /// <remarks>Although a static property, this get is actually /// a method call</remarks> public static HtmlElement BodyElement { get { return GetSingleElementByTagName("body"); } } /// <summary> /// Evaluate some JavaScript code on the page body /// </summary> public static void Eval(string code) { HtmlElement js = CreateJavaScriptElement(code); BodyElement.AppendChild(js); } /// <summary> /// Popup a JavaScript alert message /// </summary> public static void Alert(string alertMessage) { Eval("alert('" + alertMessage.Replace("'", "\\'") + "')"); } /// <summary> /// Create a JavaScript code block /// </summary> public static HtmlElement CreateJavaScriptElement(string code) { HtmlElement js = CreateJavaScriptElement(); js.SetProperty("text", code); return js; } /// <summary> /// Create an HTML element that is a JavaScript include /// </summary> public static HtmlElement CreateJavaScriptInclude(string src) { HtmlElement js = CreateJavaScriptElement(); js.SetAttribute("src", src); return js; } /// <summary> /// Create a new <script /> tag /// </summary> private static HtmlElement CreateJavaScriptElement() { HtmlElement js = HtmlPage.Document.CreateElement("script"); js.SetAttribute("type", "text/javascript"); return js; } /// <summary> /// Append multiple HtmlElements to a single HtmlElement /// </summary> public static void AppendChildren(this HtmlElement element, params HtmlElement[] children) { for (int i = 0; i < children.Length; ++i) { element.AppendChild(children[i]); } } } Hope this helps!Jeff Wilcox is a software development engineer at Microsoft who leads exciting open source projects on the Windows Azure team. Jeff has been at Microsoft 8 years and is an alumnus of the University of Michigan.
Jeff leads the open source Windows Azure SDK and cross-platform command line tools development team at Microsoft. Offering tooling for OS X, Windows and Linux and SDKs for Node.js, Java, .NET, PHP, Python; the work is open source, licensed under the Apache 2 license.
4th & Mayor is the top-rated social app on the Windows Phone Store with thousands of five star reviews. The best foursquare experience for Windows Phone, it is powered by a Node.js backend running on Windows Azure & Amazon Web Services. Jeff Wilcox is the developer of the app.