spacer

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

home / programming / javascript / vbscript
[next]
Developer News
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'

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]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Anatomy of an Ajax Application · Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags


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


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