Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript 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 Display Modes
  #1 (permalink)  
Old March 5th, 2009, 03:21 PM
Registered User
 
Join Date: Mar 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pass a Variable from VBS to JS

It is said that there are no stupid questions, but I think I may demonstrate the error of that statement.

I am so not a programmer that I am embarrassed to even be posting to this site, but desperation has made me swallow my pride.

I am currently running a VBScript as part of an automation scheme sequentially executing Excel, JS, and other routines.

I need to be able to define a variable in the VBS code that will be passed on to the JS. Typically this would be a file path. I have tried the following (but of course it does not work):

VBS code
Code:
Dim MyServerSideVariable
MyServerSideVariable = "c:\\XXXX.csv"
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run """list.js""", 6, True
set wshshell = nothing
JS code
Code:
var MyClientSideVariable = '<% =MyServerSideVariable%>';
var AB, AA;
var fso, f, f1, fc, s;
var filename;

/* create AB object */
AB = new ActiveXObject("Broker.Application");
AB.LoadDatabase("Z:\\QP database");

/* retrieve automatic analysis object */
AA = AB.Analysis;

/* load formula from external file */
AA.LoadFormula("Z:\\current list.afl");

/* run Explore*/
AA.Explore();

/* and display report */
AA.Export(MyClientSideVariable); /*<<THIS IS MY CONTRIBUTION*/

AB.quit();
Any help would be GREATLY appreciated!!!

Thanks in advance.

k
Reply With Quote
  #2 (permalink)  
Old March 5th, 2009, 03:55 PM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can you explain the environment? Are these two standalone scripts or are they running inside a web page, one as a server-side script (VBS) and the other as a client-side script (JavaScript)? I ask because the first script uses WSCript.CreateObject which won't work in a web pahge but the second has the <% %> delimiters which are used in ASP.

Also what is "list.js" or is that nothing to do with the current question?
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #3 (permalink)  
Old March 5th, 2009, 04:04 PM
Registered User
 
Join Date: Mar 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The scripts are running on my desktop (everything runs on my desktop).

list.js is the name of the file being called (with the variable being passed to it), i.e., the file shown as JS code.

Hope this answers your questions, and thanks.

k
Reply With Quote
  #4 (permalink)  
Old March 5th, 2009, 04:16 PM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Okay, firstly you will also need to get rid of the [CODEvar MyClientSideVariable = '<% =MyServerSideVariable%>';][/code]

Then you have anumber of choices. I think the easiest is to use a wsf file which can have both the VBS and the JS files included in it. You then can call the functions in one from the other. You have to make a couple of changes to wrap the code in functions though rather than just code that runs when it is hit.

Another way would be to write the variable to a file using the FileSystemObject. Then read it back from the js file.

A third way would be to use:
Code:
wshshell.run """list.js""" & " """ & MyServerSideVariable & """", 6, True
Then you could access the variable as
Code:
var MyClientSideVariable = WScript.Arguments(0)
But have you simplified the server-side example somewhat? The reason I'm asking is that you could just run the JavaScript file and pass the variable on the command line and access as above.
__________________
Joe
http://joe.fawcett.name/

Last edited by joefawcett; March 5th, 2009 at 06:02 PM.
Reply With Quote
  #5 (permalink)  
Old March 5th, 2009, 04:48 PM
Registered User
 
Join Date: Mar 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I currently run the same JS code many times during a day with the only difference being the name of the file that is being saved to. My current state of ignorance thus requires me to keep and modify many instances of what is mostly the same code whenever I make a change. I am looking to pass variables from VBS (the initiating code) to JS so as to keep the code down to a minimum.

I'm afraid that I am somewhat at a loss even with your descriptions. For example, I don't see have (in your 3'rd example) anything gets passed to the client.

I'm sure it's just me.

Sorry for being so obtuse, and thanks again.

k
Reply With Quote
  #6 (permalink)  
Old March 5th, 2009, 06:18 PM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Perhaps you can adapt these two scripts to suit your needs. Firstly PassArgs.vbs:
Code:

Dim oShell
Set oShell = CreateObject("WScript.Shell")
Dim sCommand
sCommand = """ShowArgs.js"" One Two Three"
oShell.Run sCommand, 6, True
Then ShowArgs.js:
Code:

function showArgs()
{
  var args = WScript.arguments;
  for (var i = 0, l = args.length; i < l; i++)
  {
    WScript.echo(i + ": " + args(i));
  }
}
function main()
{
  showArgs();
}
main();
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #7 (permalink)  
Old March 5th, 2009, 06:42 PM
Registered User
 
Join Date: Mar 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks,

Hopefully I'll report back with my successful solution!!


k
Reply With Quote
  #8 (permalink)  
Old March 8th, 2009, 04:09 PM
Registered User
 
Join Date: Mar 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default This works for me, though probably not the best solution

In cases where I need to pass variables in VBS to 1 or more Java scripts
Example VBS code:
Code:
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run """JS with Variable.js"" c:\FILENAME.csv", 6, True
set wshshell = nothing
wscript.sleep 30000

Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run """JS with Variable2.js"" c:\FILENAME2.csv", 6, True
set wshshell = nothing
Example JS code 1:
Code:
var MyClientSideVariable = WScript.Arguments(0);
>>>" specific code"<<<
AA.Export(MyClientSideVariable);
Example JS code 2:
Code:
var MyClientSideVariable = WScript.Arguments(0);
>>>" specific code"<<<
AA.Export(MyClientSideVariable1);
Hope this helps someone else.

If any other thought arise be happy to hear them.

k
Reply With Quote
Reply


Thread Tools
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
using js to pass selected checkbox values yasinirshad .NET Framework 1.x 0 June 3rd, 2008 02:42 AM
pass java variable to xsl variable kathy1016cats XSLT 1 June 14th, 2006 06:23 PM
how to pass an array in jsp to other js? lch Javascript 5 April 18th, 2006 06:56 AM
only do HTML/ASP when JS variable = true crmpicco Javascript How-To 1 July 19th, 2005 05:06 AM
use a JS-variable/function inside a tag cybermaarten Javascript 3 January 20th, 2005 04:42 AM



All times are GMT -4. The time now is 12:45 AM.


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