Kirk wrote:
> Let me start by saying that I am a complete idiot when it comes to
> JS.
In the words of Jamie Hyneman, "Well *there's* your problem!"
I kid, I kid.
> <snip>
> My problem is that I need the JS to execute a VB function in
> my project and I don't know how to do that. I want to do something
> like:
>
> function CheckValue(sender, args)
> {
> var sPartnumber = String(args.Value);
> if FindExistingPN(sPartNumber)
> {
> args.IsValid = false;
> return;
> }
> args.IsValid = true;
> }
>
> ...where FindExistingPN is a funciton in my VB class.
Your problem is that you're not differentiating in your head between
client-side code (JS) and server-side code (VB) in this case. The VB
you want to run is server-side, and you want to run it from client-side
code. Without some help from an AJAX framework, this is not possible to
do as seamlessly as your example.
What you need to do is send the parameter (sPartNumber) to the server in
an asynchronous request (a.k.a. AJAX, although you need not necessarily
use the XML that forms the X of this acronym), along with some sort of
meta-data that tells the server to pass it to the FindExistingPN
function and spit out the result. Then, you wait for that result,
interpret it, and decide whether or not to execute the code inside your
if-block.
In order to do this, you are going to have to wrap your head around
asynchronous operation, because your JS code will not (or at least,
should not - although it is possible) block until the result of
FindExistingPN is known. So it will have to look more like this:
Instead of
if(FindExistingPN(sPartNumber)
{
//do stuff A
return;
}
//do stuff B
you would need
var async = new XMLHttpRequest();
async.open("POST", '/special/url', true);
async.setRequestHeader('Content-Type',
'application-x-www-form-urlencoded');
async.onreadystatechange = function()
{
if(async.readyState == 4)
{
if(async.responseText == 'true')
{
//do stuff A
}
else
{
//do stuff B
}
}
}
async.send('sPartNumber=' + escape(sPartNumber));
What happens here is that you have a special URL, '/special/url', which
picks up the POST variable 'sPartNumber' and performs the check. It
prints out plain text of 'true' or 'false' depending on the result.
This gets passed into the anonymous function() which performs the stuff
you want depending on the result of the check.
The c.l.javascript FAQ has pointers to some nice AJAX resources, if you
want to pursue this option (see
http://www.jibbering.com/faq/#FAQ4_44).
You could use a framework instead (google it), but it's a good idea to
learn how it works if you want to be a good javascript developer.
Apologies for the long-winded reply.
Jeremy