spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / vbscript
[next]
Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

Using Server-Side VBScript with Client-Side JavaScript

By Jerry Gray (grgray@cmsenergy.com)

Since the first article was published I continue to receive questions on this topic. I thought it would be helpful to cover some of the most common questions that readers have had.

  1. Setting and retrieving cookies in JavaScript and reading them in VBScript (or vice-versa)
  2. Passing a variable from JavaScript to VBScript (or vice-versa)

I will also discuss why timing is so important when using these two scripting languages together.

Sharing Cookies

Using cookies is one of the easiest ways to maintain state throughout a user's session, (providing the user accepts cookies). I like to set them using client-side JavaScript, perhaps when a user has clicked on something. Let's use a fairly simple example where a user sets the background color of the page they are viewing.

The html to set this up would look something like this:


<a href="onClick()='fnSetBGColor()'">Click me to set the page color to red</a>

And the associated JavaScript function would look something like this:


<script>
function fnSetBGColor(){
	//set the background color to red
	document.bgColor = #ff0000;
}
</script>

If we want to set a cookie that remembers that the user set the bgcolor we just change our function to create a cookie called mybgcolor, and set it to the same color that we used for the background:


<script>
function fnSetBGColor(){
	//set the background color to red
	document.bgColor = "#ff0000";
	//set the cookie
	document.cookie = "mybgcolor=#ff0000";
}
</script>

If the user goes to another page on our site we can check to see if they set the background color by reading the value of the cookie, and draw our new page accordingly.

We can also use JavaScript to retrieve cookie values, but for the sake of this example we will retrieve the value using VBScript. We can read the value of our cookie using VBScript like this:


<%
mybgcolor = request.cookies("mybgcolor")
%>

Then set the background by referencing the variable in the body tag like this:


<BODY bgcolor=<%=mybgcolor%>>

Here are some more short cookie examples to set a cookie in VBScript, and to read a cookie in JavaScript.

To set a cookie in VBScript:


<%
response.cookies("mybgcolor") = "#ff0000"
%>

To read a cookie in JavaScript:


<script language=javascript>

alert(getCookie(mybgcolor));

// more cookie samples and docs available from our friends at Netscape
// http://developer.netscape.com/docs/manuals/js/client/jsguide/index.htm

function getCookie(Name) {   
	var search = Name + "="   
	if (document.cookie.length > 0) { // if there are any cookies 
	     offset = document.cookie.indexOf(search)       
		 if (offset != -1) { // if cookie exists          
		 offset += search.length          
		 // set index of beginning of value 
		 end = document.cookie.indexOf(";", offset)          
		 // set index of end of cookie value         
		 if (end == -1)             
		 	end = document.cookie.length         
			return unescape(document.cookie.substring(offset, end))      
			}    
		}
	}
</script>

As you can see, cookies do not care what language their are written in. Once a cookie exists you should be able to retrieve its value using any other language’s protocols for doing so.

home / programming / javascript / vbscript
[next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji


Created: February 12, 2001
Revised: February 21, 2001


URL: http://webreference.com/programming/javascript/vbscript/