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 .
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();
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?
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.
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.
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.
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();