Running javascript function from VBscript - possible?

Hello,

I’m working on implementing a credit card reader into a kiosk application and I need to find out if I can run javascript from within VB script.

Normally I would stick with one or the other but in this case I don’t have a choice, the mag Card reader only works with VBscript and the Kiosk application I’m working with is completely Javascript based, so switching languages is not an option.

Is there any way to get VB to run javascript functions? I’ve been trying things as simple as triggering a javascript alert with no luck.

I’m quite good at Javascript but I’m completely green when it comes to VBscript.

I’m not too good with VBScript but this should give you some ideas:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>VBScript/Javascript Communication</title>
<script type='text/javascript'>
window.onload = function()
{
  document.getElementById('form1').onsubmit = form1OnSubmit;
};
function form1OnSubmit()
{
  var v = document.getElementById("input1").value;
  vbShow(v);
  return false;
}
function jsShow(s)
{
  alert(s);
}
</script>
<script type="text/vbscript">
Function form2_OnSubmit
  Dim v
  v = document.getElementById("input2").value
  jsShow(v)
  form2_OnSubmit = False
End Function
sub vbShow(s)
  msgBox(s)
end sub
</script>
</head>
<body>
<h1>VBScript/Javascript Communication</h1>
<p><i>This demo is only for IE!</i></p>
<h2>Calling VBScript from Javascript</h2>
<form id='form1'>
<p><input id='input1' type='text' value='11111111'></p>
<p><input type='submit'></p>
</form>
<h2>Calling Javascript from VBScript</h2>
<form id='form2'>
<p><input id='input2' type='text' value='2222222'></p>
<p><input type='submit'></p>
</form>
</body>
</html>

Thanks a lot, the code you posted allowed me to figure it out. I was suprised to learn you can actually call javascript functions from within VB. Just declare the javascript function within the JS script tags, then in the VB code just call the JS function name. Like so

(I should note the kiosk app uses the script delcaration of jscript and not javascript. Don’t know if it makes a diff but I didn’t test that):

<script type=‘text/jscript’>
function myCustomFunction(){
…Do some stuff here…
}
</script>

Then the vbscript:

<script type=“text/vbscript”>
Function transferInfo
myCustomFunction()
End Function
</script>

Not sure if the jscript function name has to be enclosed in a VB function but I had some other code going and that’s what worked for me.

Anyhow thanks again and I hope this post helps some other people.

Great! :slight_smile: :tup: