Next step in the mainpage.cs inside the constructor add the code below:
When you run the application, what you will see is a message box that pops up saying Hello at the very beginning of the app.
function SetCookie(cookieName, cookieValue, Days)
var todayDate = new Date();
var expireDate = new Date();
if (Days == null || Days == 0) Days = 1;
expire.setTime(todayDate.getTime() + 3600000 * 24 * Days);
document.cookie = cookieName + ":" + cookieValue
+ ";expires=" + expireDate.toGMTString();
Next step, call the function SetCookie with this code:
HtmlPage.Window.Invoke("SetCookie", "Name", "Peter", 5);
On the code above will call SetCookie function with the parameters cookieName "Name", cookieValue "Peter" and validity, in other words Days as "5". Line #5 of the function will set the expiry time period of cookies, which is in milliseconds and is about 432000000 for 5 days. Line #6 of the function will set the cookie's information like, its Name, Value and Expiry date. Our cookie is set to give information.
Now, we want to retrieve the information. Create three buttons in the XAML of the main page, 1 for each setcookie, getcookie and deletecookie.
Copy the function on the main page to the click event of the SetCookie Button. And here is the code that I used:
var allcookies = document.cookie;
// Get all the cookies in an array
var cookiearray = allcookies.split(';');
for (var i = 0; i < cookiearray.length; i++)
var nameOfCookie = cookiearray[i].split('=');
if (cookieName == nameOfCookie)
Pass in the cookie name (which in our case is "Name") & the function can return the entire cookie. On the GetCookie button select event and we should call this function. Write the following code:
private void ButtonGet_OnClick(object sender, RoutedEventArgs e)
var cookie = HtmlPage.Window.Invoke("GetCookie", "Name");
if (cookie == null)
MessageBox.Show("No cookie found");
This will pop up a message box, showing the value of the cookie specified. Since I have my cookie as "name=Peter" it shows me "Peter".
var exp = new Date();
exp.setTime(exp.getTime() - 1);
document.cookie = cookieName + "=;expires=" + exp.toGMTString();
In the delete button click event call this function as:
private void ButtonDelete_OnClick(object sender, RoutedEventArgs e)
Pass within the name of the cookie you wish to delete and bang! it'll be deleted. currently simply do this. Click on SetCookie, it'll set the cookie for you. now click on GetCookie to verify whether or not it did set a cookie or not, you ought to see the value of the cookie within the message box. Click on Deletecookie to delete the cookie. Finally click on Getcookie button once more, if everything worked fine then you ought to see a message within the message box saying: "No cookie found".
HostForLIFE.eu Silverlight 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.