65.9K
CodeProject is changing. Read more.
Home

Silverlight JavaScript Integration - Part 1

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (3 votes)

Jul 16, 2011

CPOL
viewsIcon

23600

downloadIcon

382

Integration of Silverlight and JavaScript

Sample Image - maximum width is 600 pixels

Introduction

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.HtmlPage.Document;
                System.Windows.Browser.HtmlElement headerDiv = 
    			doc.GetElementById("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=
    		System.Windows.Browser.HtmlPage.Document;
    	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.HtmlPage.Window;
    	System.Windows.Browser.HtmlDocument doc = 
    				System.Windows.Browser.HtmlPage.Document;
    	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");
    	hDivElement.style.backgroundColor = "Green";
    }
    private void btnInvokeJSFunction_Click(object sender, RoutedEventArgs e)
    {
    	System.Windows.Browser.HtmlWindow win = 
    			System.Windows.Browser.HtmlPage.Window;
    	win.Invoke("ChangeHeaderDivColor");
    }
  • 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 = 
    			System.Windows.Browser.HtmlPage.Window;
    	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 = 
    		System.Windows.Browser.HtmlPage.Window;
    	win.Alert("Hello DOM!");
    }
  • Navigate to Link:
    private void btnNavigateToLink_Click(object sender, RoutedEventArgs e)
    {
    	string target = "_blank";
    	System.Windows.Browser.HtmlPage.Window.Navigate
    		(new Uri("http://silverlight.net"), 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)
    {
    	System.Windows.Browser.HtmlPage.Window.NavigateToBookmark("headerDiv");
    }
  • Get Browser Information:
    private void btnGetBrowserInformation_Click(object sender, RoutedEventArgs e)
    {
    	System.Windows.Browser.BrowserInformation bInfo = 
    		System.Windows.Browser.HtmlPage.BrowserInformation;
    
    	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;
    }

History

  • 16th July, 2011: Initial version