Server Side variable into client side javascript v

Results 1 to 2 of 2

Thread: Server Side variable into client side javascript v

  1. #1
    Rhett Guest

    Default Server Side variable into client side javascript v

    Have a VBScript server side variable returned from a called server object. Need to place that variable onto page so javascipt will read the returned object and implement its string client side. Trying to place the ASP code into javascript only returns the ASP code itself, can&#039t seem to get the javascript to see the actual string the asp code stands for. Any help?

  2. #2
    Join Date
    Dec 1969

    Default RE: Server Side variable into client side javascri

    &nbsp;<BR>You can&#039t put an ASP *object* into the client.<BR><BR>Remember: The *only* thing that ASP can dump out to the browser is good old fashioned HTML text. Period. (Oh, okay, unless you change to image output, etc., but that&#039s still limited to a single output type.)<BR><BR>So you have to convert the ASP to text and then, in the HTML code, possibly convert the text back into a JS object.<BR><BR>How about an example?<BR><BR>Say I have a table in a DB that has fields UserName, UserEMail, UserPhoneNumber. <BR><BR>And I want to convert each record from the DB into a JavaScript object in the browser.<BR><BR>So I&#039d do something like this:<BR><BR>&#060;HTML&#062;<BR><BR>&#060;SCRIPT Language=JavaScript&#062;<BR><BR>// create a method for our User objects:<BR>function showUser( )<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;alert( "This user is:
    "<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;+ "name : " + this.userName + "
    "<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;+ "email: " + this.userEmail + "
    "<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;+ "phone: " + this.userPhone );<BR>}<BR><BR>// the constructor for User objects:<BR>function User( uname, email, phone )<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.userName = uname;<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.userEMail = email;<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.userPhone = phone;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;this.showUse r = userShow;<BR>}<BR><BR>&#060;/SCRIPT&#062;<BR><BR>&#060;%<BR>&#039 now the ASP code (vbscript) to get the records<BR>&#039<BR>Set conn = Server.CreateObject("ADODB.Connection")<BR>conn.op en ... [ open some DB connection ] ...<BR>Set RS = conn.Execute("SELECT * FROM usersTable") &#039 get the records<BR>&#039<BR>&#039 at this point, there are lots of ways to convert that RS<BR>&#039 to text that will become a JS object, but let&#039s go with a <BR>&#039 simple one:<BR>&#039<BR>Response.Write "&#060;" & "SCRIPT Language=JavaScript&#062;" & vbNewLine<BR><BR>&#039 tell JavaScript to make an array where we will put the objects<BR>Response.Write "var allUsers = new Array( );" & vbNewLine<BR><BR>&#039 now create one JS "User" object per record in the record set<BR>&#039<BR>recNum = 0 &#039 where to put this record<BR>Do While Not RS.EOF &#039 process all records <BR>&nbsp;&nbsp;&nbsp;&nbsp;Response.Write "allUsers[" & recNum & "] = new User(&#039" & RS("userName") & "&#039,&#039" _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;& RS("userEMail") & "&#039,&#039" _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;& RS("userPhone") & "&#039);" & vbNewLine<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;recNum = recNum + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;RS.MoveNext<BR>Loop<B R><BR>...<BR><BR>%&#062;<BR>...<BR>&#060;/HTML&#062;<BR><BR>Do you see it? By using a constructor for your JS objects, and by creating those objects with inline JS code, you are able to transfer the contents using text only from the database to VBScript to client-side JavaScript.<BR><BR>The loop, above, will end up creating lines such as:<BR><BR>allUsers[0] = new User(&#039Bob Jones&#039,&,&#039 555-2737&#039);<BR>allUsers[1] = new User(&#039Harry James&#039,&,&#039 888-266-7469&#039);<BR>etc.<BR><BR>Assuming I didn&#039t make any typos in that. But if I did, they should be easy to find and fix.<BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts