Click here to Skip to main content
13,902,146 members
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 16 Jul 2011
Licenced CPOL

Silverlight JavaScript Integration - Part 1

, 16 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Integration of Silverlight and JavaScript
Sample Image - maximum width is 600 pixels


Silverlight is a browser-based plug-in. This plug-in was designed to be integrated with web pages.

Document Object Model (DOM) will allow web applications and scripts to dynamically access and update the content and schema of documents.

Using the Code

  • Access HTML elements and modify its properties:
    div id="headerDiv" style="width:100%; height:20%; background-color:Red"
    private void btnAccessHtmlElements_Click(object sender, RoutedEventArgs e)
                System.Windows.Browser.HtmlDocument doc = 
                System.Windows.Browser.HtmlElement headerDiv = 
                headerDiv.SetStyleAttribute("background-color", "green");
  • Access the value of query string.

    Suppose the web page opened with the following link:

    • http://localhost:7410/SilverlightDomInteractionTestPage.aspx?employeeID=600
    private void btnAccessQueryStrings_Click(object sender, RoutedEventArgs e)
    	System.Windows.Browser.HtmlDocument doc=
    	int empID = int.Parse(doc.QueryString["employeeID"]);
    	MessageBox.Show(empID.ToString());//Results 600
  • Access all values of query strings.

    Suppose the web page opened with the following link:

    • http://localhost:7410/SilverlightDomInteractionTestPage.aspx?employeeID=600&sallaryLessthan=2000
    private void btnAccessAllQueryStrings_Click(object sender, RoutedEventArgs e)
    	Dictionary<string, string> QueryStrings = new Dictionary<string, string>();
    	System.Windows.Browser.HtmlWindow win = 
    	System.Windows.Browser.HtmlDocument doc = 
    	foreach (string key in doc.QueryString.Keys)
    		QueryStrings.Add(key, doc.QueryString[key]);
    	MessageBox.Show(QueryStrings.Count.ToString());//Results 2
  • Invoke JavaScript function:
    function ChangeHeaderDivColor() {
    	var hDivElement = document.getElementById("headerDiv"); = "Green";
    private void btnInvokeJSFunction_Click(object sender, RoutedEventArgs e)
    	System.Windows.Browser.HtmlWindow win = 
  • Return value from JavaScript function:
    function GetSum(a, b) 
    	return a + b;
    private void btnReturnValueJSFunction_Click(object sender, RoutedEventArgs e)
    	System.Windows.Browser.HtmlWindow win = 
    	object result = win.Invoke("GetSum", 17, 7);
    	MessageBox.Show(result.ToString());//Result 24
  • Show JavaScript alert:
    private void bntJSAlert_Click(object sender, RoutedEventArgs e)
    	System.Windows.Browser.HtmlWindow win = 
    	win.Alert("Hello DOM!");
  • Navigate to Link:
    private void btnNavigateToLink_Click(object sender, RoutedEventArgs e)
    	string target = "_blank";
    		(new Uri(""), target);
    _blank	Open the linked document in a new window or tab
    _self	Open the linked document in the same frame as it was clicked 
    	(this is default)
    _parent	Open the linked document in the parent frameset
    _top	Open the linked document in the full body of the window
    framename	Open the linked document in a named frame
  • Navigate to Bookmark:
    private void btnNavigateToBookmark_Click(object sender, RoutedEventArgs e)
  • Get Browser Information:
    private void btnGetBrowserInformation_Click(object sender, RoutedEventArgs e)
    	System.Windows.Browser.BrowserInformation bInfo = 
    	int majorVersion = bInfo.BrowserVersion.Major;
    	int minorVersion = bInfo.BrowserVersion.Minor;
    	int revisionVersion = bInfo.BrowserVersion.Revision;
    	int buildVersion = bInfo.BrowserVersion.Build;
    	bool cookiesEnabled = bInfo.CookiesEnabled;
    	string name = bInfo.Name;
    	string platform = bInfo.Platform;
    	string productName = bInfo.ProductName;
    	string productVersion = bInfo.ProductVersion;
    	string userAgent = bInfo.UserAgent;


  • 16th July, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Jordan Jordan
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 16 Jul 2011
Article Copyright 2011 by alrosan
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid