Source language: Translate to:

JScript syntax error on setting var

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

JScript syntax error on setting var

Postby dpayer » Wed Jan 30, 2013 10:02 am

I have a webpage that loads into a NB webbrowser object. I want to capture the form values of it as NB variables.

I use a JScript function that executes on the clicking of the submit button. It gets the value of each element id within the form named WebBrowser1.
Code: Select all
<html>
<head>
<title>Webpage Form</title>
<script>
function getFORMvalues()
{
var x=document.getElementById("WebBrowser1");
for (var i=0;i<x.length;i++)
window.alert(x.elements[i].value);
nbSetVar ( 'formarray[i]', x.elements[i].value );
}
</script>
</head>
<body style="color: blue; font-family: Arial;">
<center><form id="WebBrowser1" name="WebBrowser1" method="post" action="javascript:getFORMvalues()"><table width="100%" border="0">
<tr><td width="50%"><div align="right" id="WFFlabel">Name</div></td><td width="50%"><div align="left" id="WFFDataCol"><input type="text" name="text1"
id="text1" size="" /></div></td></tr>
<tr><td width="50%"><div align="right" id="WFFlabel">address</div></td><td width="50%"><div align="left" id="WFFDataCol"><input type="text"
name="text2" id="text2" size="" /></div></td></tr>
<tr><td width="50%"><div align="right" id="WFFlabel">city</div></td><td width="50%"><div align="left"  id="WFFDataCol"><input type="text" name="text3" id="text3" size="" /></div></td></tr>
<tr><td width="50%"><div align="right" id="WFFlabel">phone</div></td><td width="50%"><div align="left" id="WFFDataCol"><input type="text" name="text4" id="text4" size="" /></div></td></tr>
</table><p><input type="submit"  id="Submit" value="Submit" />
</form>
</center>
</body>
</html>


The NB variable does not get set. I inserted an alert box to see if I was generating the value properly and I am (the values entered into the form are shown in the alert box for each loop) but the nbSetVar command does not create/populate the NB variable in the NB app.

I have used window.external.nbSetVar and just nbSetVar. I have put the script in the header and in the body. These things are not the issue but I must have some syntax wrong.

If anyone has an insight as to my error, I'd appreciate hearing it.

David P.
David Payer
Des Moines, Iowa
USA
User avatar
dpayer
 
Posts: 1382
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: JScript syntax error on setting var

Postby Gaev » Wed Jan 30, 2013 10:48 am

David P.:

I don't know why you don't have { } around the commands tp be serviced for each iteration in the for command ... and I don't know if the short form nbSetVar works.

But apart from that, the problem is most likely your use of 'formarray[i]' ... since you placed (the required) quotes around the parameter specification ... javascript will leave everything inside it 'as-is' ... so you end up asking to set a variable whose name includes the square brackets and the literal i... I do not believe you can have such 'NeoBook Variable Names'.

So what you need to do is ask to populate variables ...

formarray1
formarray2
formarray3
etc.

... so try this ...

Code: Select all
var x=document.getElementById("WebBrowser1");
for (var i=0;i<x.length;i++)
{
   window.alert(x.elements[i].value);
   thisNeoBookVariable = 'formarray' + i;
   nbSetVar ( thisNeoBookVariable, x.elements[i].value );
}
User avatar
Gaev
 
Posts: 3728
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: JScript syntax error on setting var

Postby dpayer » Wed Jan 30, 2013 12:05 pm

Thanks for commenting Gaev. When I simply have the alertbox in (remove the nbSetVar line) the script works correctly. It pops up the value of each element as it goes through the loop. I don't think the squiggly brackets is the issue. In fact, this script is based off a W3school sample.

My hope was to loop through the values and use the counter variable in JScript to also be the VariableArray number for NB.

I am wondering if it is expected usage with an in-page JScript function (called by jscript via a button) to set a NB variable as I tried to set an NB var with a static value and that does not appear to work either using either the short or full (window.external.nbSetVar) version of nbSetVar. I know this process works when I use a NB Function (of JS type). This is whats odd. I am relying on the NB debugger to tell me if the variable is created/populated. I am not seeing that happen in the debugger

I adapted your suggestions for the script:
Code: Select all
<script language="JScript">
function getFORMvalues()
{
var x=document.getElementById("WebBrowser1");
for (var i=0;i<x.length;i++)
window.alert(x.elements[i].value);
thisNeoBookVariable = 'formarray' + i;
window.external.nbSetVar ( '[thisNeoBookVariable]', x.elements[i].value );
}
</script>

It does do the alert popup with the field value but does not set any NB variable.

I'll keep looking. Thanks for your input.

David P.
David Payer
Des Moines, Iowa
USA
User avatar
dpayer
 
Posts: 1382
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: JScript syntax error on setting var

Postby dec » Wed Jan 30, 2013 12:24 pm

Hello,

dpayer wrote:Thanks for commenting Gaev. When I simply have the alertbox in (remove the nbSetVar line) the script works correctly. It pops up the value of each element as it goes through the loop. I don't think the squiggly brackets is the issue. In fact, this script is based off a W3school sample.


But "eagle-eye-Gaev" :) have reason David, in this code:

Code: Select all
for( var i = 0; i <= 100; i++)
  alert( 'Hello!' );
alert( 'Good bye!' );


... the second alert only is executed one time: just when the loop end. This is the construction of the language, if you do not specify the brackets only the next instruction are executed, so, if we expect the two alerts we need to write something like this:

Code: Select all
for( var i = 0; i <= 100; i++)
{
  alert( 'Hello!' );
  alert( 'Good bye!' );
}


And this is valid also for others language statements like "if", "while", etc. And not only in Javascript, also in C++ or Delphi, for example:

Code: Select all
foreach( s in t )
  ShowMessage( s );
ShowMessage( 'This is executed only one time... not in every iteration' );


Which can be writen like this in Delphi (Pascal):

Code: Select all
foreach( s in t )
begin
  ShowMessage( s );
  ShowMessage( 'This is also executed every iteration' );
end;


Just that in this case the brackets is substituted by "begin" and "end".
Last edited by dec on Wed Jan 30, 2013 12:27 pm, edited 1 time in total.
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: JScript syntax error on setting var

Postby Neosoft Support » Wed Jan 30, 2013 12:25 pm

This will work:

Code: Select all
function getFORMvalues()
{
var x=document.getElementById("WebBrowser1");
for (var i=0;i<x.length;i++)
  {
    thisNeoBookVariable = 'formarray' + i;
    window.external.nbSetVar ( '['+thisNeoBookVariable+']', x.elements[i].value );
  }
}
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5602
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Re: JScript syntax error on setting var

Postby dpayer » Wed Jan 30, 2013 2:58 pm

Thanks all for your input.

This makes me painfully aware that my jscript skills are elementary. David (DEC) thanks for the { } clarification of Gaev's comments. Dave (NeosoftSupport) thanks for the example. I had not seen the use of plus signs before in any example. Could you point me to a resource to understand how they are used and why they are needed in this case? I did not see them used in the W3Schools site. Is it a NB specific situation?

David P.
David Payer
Des Moines, Iowa
USA
User avatar
dpayer
 
Posts: 1382
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: JScript syntax error on setting var

Postby dec » Thu Jan 31, 2013 1:02 am

Hello,

I cannot remember what article, tutorial or book (and for what language) tell me the use of the "brackets" or "blocks" in certain control structures. The only thing I can say about is that this is widely used, at the least, in the languages that I can know. But don't worry David, the exactly mistake are made (I'am sure, because I'am moderator of a Delphi forum from years ago) by many people, included me, of course. Some people always use the "brackets" or the appropiate "block" (like "begin" and "end") in order to prevent this kind of mistakes. ;)
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: JScript syntax error on setting var

Postby Gaev » Thu Jan 31, 2013 7:16 am

David P:
I had not seen the use of plus signs before in any example. Could you point me to a resource to understand how they are used and why they are needed in this case? I did not see them used in the W3Schools site. Is it a NB specific situation?

In Javascript you use plus signs to either add (numeric variables) or concatenate (string variables) e.g.

Code: Select all
males = 49;
females = 51;
population = males + females; //population will contain 100

firstName = 'John";
lastName = "Smith"
fullName = firstname + ' ' + lastName; //fullName will contain John Smith

So what Dave's code does is set/pass the value of the first parameter as [formarray1], [formarray2] etc. etc. ... I think this (easier to understand) code will do the same ...
Code: Select all
for (var i=0;i<x.length;i++)
  {
    thisNeoBookVariable = '[formarray' + i + ']';
    window.external.nbSetVar ( thisNeoBookVariable, x.elements[i].value );
  }

The key point in all this is that ...

1) In my first post, I had neglected to check the Help file ... which clearly states that ...
The variable name should be surrounded by square brackets.

2) NeoBook Array Variables are just 'simple variables with a numeric suffix'

While NeoBook is kind enough to allow you to access elements of such array variables using syntax like [Name3] or [Name[x]] where x is another variable ... javascript is not so accomodating ... anything you specify within (single or double) quotes is passed 'as-is' ... hence the need to 'resolve the value of i on the javascript side.



@dec:

I did not know that you could skip the {} when you have 'just one command in the for loop' ... I always use such braces in all block commands ... if, else if, else, for, while, do etc. ... much easier to read/maintain as well.
User avatar
Gaev
 
Posts: 3728
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: JScript syntax error on setting var

Postby dec » Thu Jan 31, 2013 8:04 am

Hello,

Gaev wrote:@dec:

I did not know that you could skip the {} when you have 'just one command in the for loop' ... I always use such braces in all block commands ... if, else if, else, for, while, do etc. ... much easier to read/maintain as well.


I agree. I almost always do the same thing, except on rare occasions, and for the two reasons: avoid this kind of "mistakes" and gain a better code readability. ;)
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain


Return to NeoBook Functions - VBScript & JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest