Introduction
My doggie love cookies, I love my doggie, this implies I love cookies. Ah! A bad joke is it? Never mind, you will be reading more about me and my bad jokes throughout my articles. That doesn't mean you should stop reading them and go away…after all, they are very informative and cool. Anyways, over with the jokes.
Cookies are data stored by the web browser, as simple as that. You can save anything; yes I said anything, in cookies. We can also do this through Silverlight itself, but for fun let's try doing it with JavaScript. In this process we follow, we will be accessing JavaScript's function from Silverlight. This sometimes becomes a headache for most developers, but you should not worry, you are reading "my article".
The first step towards setting cookies through JavaScript is to call the JavaScript function from Silverlight. Calling a JavaScript function from Silverlight is very easy. To understand this, and more forthcoming things, create a new Silverlight application "JavaScriptTweaks". Open JavaScriptTweaksTestpage.aspx, and add the following code somewhere inside the <head> tag:
- <script type="text/javascript">
- function SayHello() {
- alert("Hello!");
- }
- </script>
Now on the mainpage.cs inside the constructor add the following line:
Okay, what we have done so far is, we have created a JavaScript function SayHello and called it from Silverlight. When you run the application, what you will see is a message box that pops up saying Hello at the very beginning of the application. Congratulations! You've just learned how to call a JavaScript function from Silverlight.
The Second Step
You might be so happy after completing the first step. And that's no problem indeed. The actual problem is that the next step is a little bit complex. It's the Cookie functions.
Setting Cookie
Remove the SayHello function from the JavaScript we wrote previously and add the following function instead.
- HtmlPage.Window.Invoke("SayHello");
- 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();
- }


Gowtham RajamanickamPosted May 20, 2015, 1:02 AM
good