Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2004, 06:25 AM
PL PL is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using VBScript variables in Javascript

Hi!

Can anyone tell me if and how it is possible to pass the contents of an asp vbscript variable to a javascript variable ?

As I'm familiar with javascript I don't want to abandon it when I use asp but I would like to use vbscript session and application variables to add some multi-user functionality to some scripts.

I know its possible to use javascript scripts within asp files and even write out asp vbscript variables within a javascript section of code but it would be very useful if the <%=variable%> could somehow be used by javascript.

As asp seems to fit in well with html I thought it might be possible to assign the variable to a form field and then read it somehow from there. (Either by using location.search in a response page after the form has been submitted or by reading the form field with javscript in the usual way). I haven't been able to make either of these work.

Failing this - does jscript have anything equivalent to session and applicationvariables and if so is it be possible to pass the variables to javascript in this case ?

Thanks
 
Old July 25th, 2004, 06:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I am not sure I completely understand your requirements. Should all this be happening client side? Or when you say VBScript do you actually mean "server side ASP VBScript" and when you say JavaScript do you mean "client side JavaScript"?

There is a big difference between the two; you cannot directly access server side variables from client side code and vice versa. If you elaborate a bit about what exactly you want to do, I can give you some more useful advice or help.

Cheers,

Imar
 
Old July 25th, 2004, 09:08 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi PL,

Thank you for your personal message, but could you please post replies here instead of e-mailing me directly? The whole idea of a forum is to create a centralized location for questions and answers.... ;)

If you need to pass values of variables to client side JavaScript, try something like this:
Code:
<!-- Server Side ASP Code<%
  Dim MyServerSideVariable
  MyServerSideVariable = "Test Value"
%>

<script type="text/javascript">
  var MyClientSideVariable = '<%MyServerSideVariable%>';
</script>
This puts the words Test Value into the client side variable, so it ends up like this:

  var MyClientSideVariable = 'Test Value';

From this point on, you can the variable as you normally would.

If you need to roundtrip the client side value back into the server side variable, you need to put it in a field (text or hidden for example) and then submit the page back to the server.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Yuri-G by P.J. Harvey (Track 9 from the album: Rid Of Me) What's This?
 
Old July 29th, 2004, 02:21 PM
PL PL is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Re the example script for assigning ASP server side variable values to client side Javascript variables.

<!-- Server Side ASP Code<%
  Dim MyServerSideVariable
  MyServerSideVariable = "Test Value"
%>

<script type="text/javascript">
  var MyClientSideVariable = <%MyServerSideVariable%>';
</script>


The above code does work but with one small change.

var MyClientSideVariable = '<% =MyServerSideVariable%>';

I know it looks funny with two = like a double assignment but
as you can see, the first equals is a javascript assignment and the
second is an asp ‘output’. I couldn’t get this to work for a
while because some of my opening and closing tags were in the
wrong place. Something which seems to be not uncommon !

Anyway – thanks. It works now.
 
Old July 29th, 2004, 03:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right, my bad. I must have typed this message to quickly.

You are absolutely right. The first = is required by the client side JavaScript, while the other is a shortcut for Response.Write.


Sorry about that.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Right As Rain by dEUS (Track 7 from the album: Worst Case Scenario) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript to VBScript Thomas82 Classic ASP Basics 1 July 29th, 2006 07:03 PM
Javascript and VBScript woes 1up Javascript How-To 18 November 7th, 2005 03:18 PM
Mixing JavaScript and VBScript in ASP badgolfer ASP.NET 1.0 and 1.1 Basics 2 September 10th, 2004 09:34 AM
vbscript and javascript together erin VBScript 1 October 21st, 2003 02:48 PM
Using VBScript and JavaScript together jwalborn Classic ASP Professional 2 October 13th, 2003 10:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.