Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 25th, 2004, 06:25 AM
PL PL is offline
Registered User
 
Join Date: Jul 2004
Location: , , .
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
Reply With Quote
  #2 (permalink)  
Old July 25th, 2004, 06:58 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 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
Reply With Quote
  #3 (permalink)  
Old July 25th, 2004, 09:08 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 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?
Reply With Quote
  #4 (permalink)  
Old July 29th, 2004, 02:21 PM
PL PL is offline
Registered User
 
Join Date: Jul 2004
Location: , , .
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.
Reply With Quote
  #5 (permalink)  
Old July 29th, 2004, 03:05 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 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?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:37 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.