Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: Accessing variables across VBS to JavaScript


Message #1 by "Riia" <riia.mattila@m...> on Thu, 8 Nov 2001 09:54:03
We have JavaScript code for creating a dynamic menubar (well, pretty 

static for now as the menu items are hard coded). Anyway, we'd like to 

change the menubar more dynamic by displaying there only the sites the 

user has access to. I.e. the contents of the menubar would 

depend on the user rights. The menu items would probably be figured out in 

a component or in a server-side script. As we use VBScript for server-side 

processing we should then provide the value of the variable holding the 

menuitems (array, string?) to the client-side Javascript. One possibility 

would be to save the variable in a session object, but is there a way to 

access it from the client-side JavaScript code?



Anyway, the code below seems to work well with numbers&strings etc. but 

what about arrays? So far I've only managed to get "Type mismatch" 

and "Subscript out of range". 



<%

  Dim VBVar

  VBVar = 2

%>



<script language="JavaScript">

  var JSVar;



  JSVar = <%=VBVar%>;

</script>



Yet another thing: The example works well if I have the JavaScript code 

inside the <script>-tags on my ASP-page. What if my JavaScript code is in 

a separate js-file like



<script language="javascript" src="../Includes/JavaScript/FileName.js">



Now that will go beyond the variable scope, right? 



I wouldn't want to spend too much time on this as the functionality of the 

pages is the main aim right now and the menubar is considered to 

be "extra". Still I wouldn't want to end up having a "static" menubar with 

hard coded items as that would mean a) having links only to pages that 

have free access to everyone or b) having all the links in the menubar 

which might lead customers to "access denied"-pages.



Is there an easy and reasonable way of doing this? 



Thanking you in advance,



Riia
Message #2 by "phil griffiths" <pgtips@m...> on Thu, 8 Nov 2001 10:14:05
Looks like you're on the right track - using server side code to create 

client-side js is fine.  There's no way to access a server-side session 

var from client-side js.



If u want to pass an array to the client just use the same method - server-

side code which creates a client-side js array, e.g

<% 

response.write "var clientarray = new array();"

for i = lbound(serverarray) to ubound(serverarray)

response.write "clientarray[" & i & "] = " & serverarray(i) & ";"

next 

%>



As long as these js vars have page-level scope you should be able to 

access them in included js.



HTH

Phil

> We have JavaScript code for creating a dynamic menubar (well, pretty 

> static for now as the menu items are hard coded). Anyway, we'd like to 

> change the menubar more dynamic by displaying there only the sites the 

> user has access to. I.e. the contents of the menubar would 

> depend on the user rights. The menu items would probably be figured out 

in 

> a component or in a server-side script. As we use VBScript for server-

side 

> processing we should then provide the value of the variable holding the 

> menuitems (array, string?) to the client-side Javascript. One 

possibility 

> would be to save the variable in a session object, but is there a way to 

> access it from the client-side JavaScript code?

> 

> Anyway, the code below seems to work well with numbers&strings etc. but 

> what about arrays? So far I've only managed to get "Type mismatch" 

> and "Subscript out of range". 

> 

> <%

>   Dim VBVar

>   VBVar = 2

> %>

> 

> <script language="JavaScript">

>   var JSVar;

> 

>   JSVar = <%=VBVar%>;

> </script>

> 

> Yet another thing: The example works well if I have the JavaScript code 

> inside the <script>-tags on my ASP-page. What if my JavaScript code is 

in 

> a separate js-file like

> 

> <script language="javascript" src="../Includes/JavaScript/FileName.js">

> 

> Now that will go beyond the variable scope, right? 

> 

> I wouldn't want to spend too much time on this as the functionality of 

the 

> pages is the main aim right now and the menubar is considered to 

> be "extra". Still I wouldn't want to end up having a "static" menubar 

with 

> hard coded items as that would mean a) having links only to pages that 

> have free access to everyone or b) having all the links in the menubar 

> which might lead customers to "access denied"-pages.

> 

> Is there an easy and reasonable way of doing this? 

> 

> Thanking you in advance,

> 

> Riia
Message #3 by "Ken Schaefer" <ken@a...> on Thu, 8 Nov 2001 22:35:46 +1100
You can't do:



javascriptArray = <%=VBScriptArray%>



You need to iterate the VBScript array and explictly assign each element of

the VBScript array to the corresponding element of the Javascript array.



Remember, the javascript is processed on the client *after* all the

server-side processing is done. The Javascript on the client knows nothing

about the VBscript array:



javescriptArray[<%=i%>] = <%=VBScriptArray(i-1)%>



etc



Cheers

Ken



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: "Riia" <riia.mattila@m...>

To: "ASP Web HowTo" <asp_web_howto@p...>

Sent: Thursday, November 08, 2001 9:54 AM

Subject: [asp_web_howto] Accessing variables across VBS to JavaScript





: We have JavaScript code for creating a dynamic menubar (well, pretty

: static for now as the menu items are hard coded). Anyway, we'd like to

: change the menubar more dynamic by displaying there only the sites the

: user has access to. I.e. the contents of the menubar would

: depend on the user rights. The menu items would probably be figured out in

: a component or in a server-side script. As we use VBScript for server-side

: processing we should then provide the value of the variable holding the

: menuitems (array, string?) to the client-side Javascript. One possibility

: would be to save the variable in a session object, but is there a way to

: access it from the client-side JavaScript code?

:

: Anyway, the code below seems to work well with numbers&strings etc. but

: what about arrays? So far I've only managed to get "Type mismatch"

: and "Subscript out of range".

:

: <%

:   Dim VBVar

:   VBVar = 2

: %>

:

: <script language="JavaScript">

:   var JSVar;

:

:   JSVar = <%=VBVar%>;

: </script>

:

: Yet another thing: The example works well if I have the JavaScript code

: inside the <script>-tags on my ASP-page. What if my JavaScript code is in

: a separate js-file like

:

: <script language="javascript" src="../Includes/JavaScript/FileName.js">



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Message #4 by "Riia" <riia.mattila@m...> on Thu, 8 Nov 2001 14:23:49
Thank you guys, you really made me see the wood for trees! There's really 

no point in creating complicated arrays or whatever when actually what I 

only need to do is to create the snippet of JavaScript code with VBScript! 

In my case, I'll just use Response.Write to create the code



Menu2=new Array("News","blank.htm",2);

Menu2_1=new Array("General","blank.htm",5,20,150);	

Menu2_1_1=new Array("CNN","http://www.cnn.com",0,20,150);



...etc.



It's as simple as that. Well, you can easily tell that "I was born 

yesterday" when it comes to JavaScript, but this is all very interesting 

and I'm eager to learn more.



Anyway, thanks a lot for your help, you really made my day!



Riia
Message #5 by Kyle Burns <kburns@c...> on Thu, 8 Nov 2001 10:48:22 -0500
Along this thread, I'm getting ready to do some reworking of our site.

Anyone know of a good resource for cross-browser dynamic menus?





=================================

Kyle M. Burns, MCSD, MCT

ECommerce Technology Manager

Centra Credit Union

kburns@c...



 



-----Original Message-----

From: Riia [mailto:riia.mattila@m...]

Sent: Thursday, November 08, 2001 9:24 AM

To: ASP Web HowTo

Subject: [asp_web_howto] Re: Accessing variables across VBS to

JavaScript





Thank you guys, you really made me see the wood for trees! There's really 

no point in creating complicated arrays or whatever when actually what I 

only need to do is to create the snippet of JavaScript code with VBScript! 

In my case, I'll just use Response.Write to create the code



Menu2=new Array("News","blank.htm",2);

Menu2_1=new Array("General","blank.htm",5,20,150);	

Menu2_1_1=new Array("CNN","http://www.cnn.com",0,20,150);



...etc.



It's as simple as that. Well, you can easily tell that "I was born 

yesterday" when it comes to JavaScript, but this is all very interesting 

and I'm eager to learn more.



Anyway, thanks a lot for your help, you really made my day!



Riia




$subst('Email.Unsub')

Message #6 by "Alex Shiell, ITS, EC, SE" <alex.shiell@s...> on Thu, 8 Nov 2001 16:06:34 -0000
yes - http://www.webreference.com/dhtml/hiermenus/



-----Original Message-----

From: Kyle Burns [mailto:kburns@c...]

Sent: 08 November 2001 15:48

To: ASP Web HowTo

Subject: [asp_web_howto] Re: Accessing variables across VBS to JavaScr

ipt





Along this thread, I'm getting ready to do some reworking of our site.

Anyone know of a good resource for cross-browser dynamic menus?





=================================

Kyle M. Burns, MCSD, MCT

ECommerce Technology Manager

Centra Credit Union

kburns@c...



 



-----Original Message-----

From: Riia [mailto:riia.mattila@m...]

Sent: Thursday, November 08, 2001 9:24 AM

To: ASP Web HowTo

Subject: [asp_web_howto] Re: Accessing variables across VBS to

JavaScript





Thank you guys, you really made me see the wood for trees! There's really 

no point in creating complicated arrays or whatever when actually what I 

only need to do is to create the snippet of JavaScript code with VBScript! 

In my case, I'll just use Response.Write to create the code



Menu2=new Array("News","blank.htm",2);

Menu2_1=new Array("General","blank.htm",5,20,150);	

Menu2_1_1=new Array("CNN","http://www.cnn.com",0,20,150);



...etc.



It's as simple as that. Well, you can easily tell that "I was born 

yesterday" when it comes to JavaScript, but this is all very interesting 

and I'm eager to learn more.



Anyway, thanks a lot for your help, you really made my day!



Riia




$subst('Email.Unsub')






$subst('Email.Unsub')



________________________________________________________________________

Scottish Enterprise Network

http://www.scottish-enterprise.com



Headquarters Address & Contact Numbers



150 Broomielaw

5 Atlantic Quay

Glasgow

G2 8LU.

Tel:  +44 (0) 141 248 2700.

Fax:  +44 (0)141 221 3217



 This message is sent in confidence for the addressee only.

It may contain legally privileged information. The contents are not to

be disclosed to anyone other than the addressee. Unauthorised recipients

are requested to preserve this confidentiality and to advise the sender

immediately of any error in transmission.





Message #7 by "Riia" <riia.mattila@m...> on Thu, 8 Nov 2001 16:52:41
The one we're using can be found from http://www.dynamicdrive.com



Though they may claim they work on "all" (IE4+ & NS4; some on NS6) 

browsers, might be a good idea to check them on all browser versions (as 

far as possible). Luckily our pages are for restricted use and need to 

work only on IE4+.



Riia





> Along this thread, I'm getting ready to do some reworking of our site.

> Anyone know of a good resource for cross-browser dynamic menus?

> 

> 

> =================================

> Kyle M. Burns, MCSD, MCT

> ECommerce Technology Manager

> Centra Credit Union

> kburns@c...

> 

Message #8 by Tarey Gettys <tareyg@y...> on Thu, 8 Nov 2001 15:41:34 -0800 (PST)
Will this work:

 javescriptArray[<%=i%>] = <%=VBScriptArray(i-1)%> 



if VBScriptArray is a multiple dimension array? I

thought java arrays were only single dimension.



Tarey

--- Ken Schaefer <ken@a...> wrote:

> You can't do:

> 

> javascriptArray = <%=VBScriptArray%>

> 

> You need to iterate the VBScript array and explictly

> assign each element of

> the VBScript array to the corresponding element of

> the Javascript array.

> 

> Remember, the javascript is processed on the client

> *after* all the

> server-side processing is done. The Javascript on

> the client knows nothing

> about the VBscript array:

> 

> javescriptArray[<%=i%>] = <%=VBScriptArray(i-1)%>

> 

> etc

> 

> Cheers

> Ken

> 

> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> From: "Riia" <riia.mattila@m...>

> To: "ASP Web HowTo" <asp_web_howto@p...>

> Sent: Thursday, November 08, 2001 9:54 AM

> Subject: [asp_web_howto] Accessing variables across

> VBS to JavaScript

> 

> 

> : We have JavaScript code for creating a dynamic

> menubar (well, pretty

> : static for now as the menu items are hard coded).

> Anyway, we'd like to

> : change the menubar more dynamic by displaying

> there only the sites the

> : user has access to. I.e. the contents of the

> menubar would

> : depend on the user rights. The menu items would

> probably be figured out in

> : a component or in a server-side script. As we use

> VBScript for server-side

> : processing we should then provide the value of the

> variable holding the

> : menuitems (array, string?) to the client-side

> Javascript. One possibility

> : would be to save the variable in a session object,

> but is there a way to

> : access it from the client-side JavaScript code?

> :

> : Anyway, the code below seems to work well with

> numbers&strings etc. but

> : what about arrays? So far I've only managed to get

> "Type mismatch"

> : and "Subscript out of range".

> :

> : <%

> :   Dim VBVar

> :   VBVar = 2

> : %>

> :

> : <script language="JavaScript">

> :   var JSVar;

> :

> :   JSVar = <%=VBVar%>;

> : </script>

> :

> : Yet another thing: The example works well if I

> have the JavaScript code

> : inside the <script>-tags on my ASP-page. What if

> my JavaScript code is in

> : a separate js-file like

> :

> : <script language="javascript"

> src="../Includes/JavaScript/FileName.js">

> 

> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> 

> 



> tareyg@y...


> $subst('Email.Unsub')

> 





__________________________________________________

Do You Yahoo!?

Find a job, post your resume.

http://careers.yahoo.com

Message #9 by "Ken Schaefer" <ken@a...> on Fri, 9 Nov 2001 12:21:02 +1100
Actually it's pseudo-code, and bad psuedo-code at that :-)



It should be something like:



javascriptArray[<%=i%>] = <%=VBScriptArray(i)%>;



and if you have a multidimensional VBScript array, you could create a number

of corresponding client-side arrays - not being a javascript expert, I'm

hesitant to suggest an array of arrays, but that might be possible as well.



Cheers

Ken



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: "Tarey Gettys" <tareyg@y...>

Subject: [asp_web_howto] Re: Accessing variables across VBS to JavaScript





: Will this work:

:  javescriptArray[<%=i%>] = <%=VBScriptArray(i-1)%>

:

: if VBScriptArray is a multiple dimension array? I

: thought java arrays were only single dimension.

:

: Tarey





Message #10 by "Vinay Poddar" <vinay.poddar@s...> on Fri, 9 Nov 2001 12:10:45 +0530

Thanks phil, its great explanation you have given.



Regards,











Message #11 by "Riia" <riia.mattila@m...> on Fri, 9 Nov 2001 07:49:16
As I'm new to JavaScript, this came as a bit of a shock for me. What's the 

point in having such a restriction? The need for, at least, 2-dimensional 

arrays is pretty common, JavaScript doesn't make it too easy for you, does 

it? Well, this is what I found:



Quote from http://developer.irt.org/script/365.htm:



JavaScript does not support real multi-dimensional arrays. You can however 

simulate multi-dimensional arrays using two different techniques: 



<SCRIPT LANGUAGE="JavaScript"><!--

//  Arrays within arrays:



var x = new Array(new Array(1,2,3),new Array('A','B','C'),new Array

('x','y','z'));



alert(x[1][2]);  // returns 'C'



//  Associative arrays:



var myArray = new Array();

myArray['00'] = 1;

myArray['01'] = 2;

myArray['02'] = 3;

myArray['10'] = 'A';

myArray['11'] = 'B';

myArray['12'] = 'C';

myArray['20'] = 'x';

myArray['21'] = 'y';

myArray['22'] = 'z';



alert(myArray['1' + '2']);  // returns 'C';

//--></SCRIPT>



 

Riia





> Will this work:

>  javescriptArray[<%=i%>] = <%=VBScriptArray(i-1)%> 

> 

> if VBScriptArray is a multiple dimension array? I

> thought java arrays were only single dimension.

> 

> Tarey

Message #12 by "Alex Shiell, ITS, EC, SE" <alex.shiell@s...> on Fri, 9 Nov 2001 11:14:14 -0000
javascript can do better though because what you can do is use a constructor

to create objects and store an array of objects.  This means that your

individual items can be accessed as properties rather than having to

remember which index represents each one.



for example if it was to represent a database table your constructor would

be:



function tablerow(sField1, sField2, sField3){

	this.field1 = sField1;

	this.field2 = sField2;

	this.field3 = sField3;

}



then you can build your array like this



aArray[0]= new tablerow(1,"A",true);

aArray[1]= new tablerow(5,"T",false);

aArray[2]= new tablerow(2,"R",true);



you can the retreive the individual values like this



aArray[2].field3







-----Original Message-----

From: Riia [mailto:riia.mattila@m...]

Sent: 09 November 2001 07:49

To: ASP Web HowTo

Subject: [asp_web_howto] Re: Accessing variables across VBS to

JavaScript





As I'm new to JavaScript, this came as a bit of a shock for me. What's the 

point in having such a restriction? The need for, at least, 2-dimensional 

arrays is pretty common, JavaScript doesn't make it too easy for you, does 

it? Well, this is what I found:



Quote from http://developer.irt.org/script/365.htm:



JavaScript does not support real multi-dimensional arrays. You can however 

simulate multi-dimensional arrays using two different techniques: 



<SCRIPT LANGUAGE="JavaScript"><!--

//  Arrays within arrays:



var x = new Array(new Array(1,2,3),new Array('A','B','C'),new Array

('x','y','z'));



alert(x[1][2]);  // returns 'C'



//  Associative arrays:



var myArray = new Array();

myArray['00'] = 1;

myArray['01'] = 2;

myArray['02'] = 3;

myArray['10'] = 'A';

myArray['11'] = 'B';

myArray['12'] = 'C';

myArray['20'] = 'x';

myArray['21'] = 'y';

myArray['22'] = 'z';



alert(myArray['1' + '2']);  // returns 'C';

//--></SCRIPT>



 

Riia





> Will this work:

>  javescriptArray[<%=i%>] = <%=VBScriptArray(i-1)%> 

> 

> if VBScriptArray is a multiple dimension array? I

> thought java arrays were only single dimension.

> 

> Tarey






$subst('Email.Unsub')



________________________________________________________________________

Scottish Enterprise Network

http://www.scottish-enterprise.com



Headquarters Address & Contact Numbers



150 Broomielaw

5 Atlantic Quay

Glasgow

G2 8LU.

Tel:  +44 (0) 141 248 2700.

Fax:  +44 (0)141 221 3217



 This message is sent in confidence for the addressee only.

It may contain legally privileged information. The contents are not to

be disclosed to anyone other than the addressee. Unauthorised recipients

are requested to preserve this confidentiality and to advise the sender

immediately of any error in transmission.






  Return to Index