Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 5th, 2009, 04: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
 
Old March 5th, 2009, 04:55 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
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/
 
Old March 5th, 2009, 05: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
 
Old March 5th, 2009, 05:16 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
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 07:02 PM..
 
Old March 5th, 2009, 05: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
 
Old March 5th, 2009, 07:18 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
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/
 
Old March 5th, 2009, 07: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
 
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





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 05:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.