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 September 15th, 2008, 03:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Calling JS Function from VS?

What is the proper syntax for calling a Javascript function from VBScript? I keep getting a &^%$# TYPE MISMATCH errorand my code is nothing more complex than this:

<VBSCRIPT>
sub StatusButtonScript()

dim strResult
  strResult = IsMCS()
end sub
<Javascript>

function IsMCS()
{
alert('!');
}

What could possibly be generating a TYPE MISMATCH?

PLEASE READ THIS BEFORE RESPONDING: If you have nothing to offer except a lecture on the superiority of Javascript, do not waste your time or mine. I am in an all IE world where I can use VBScript to my heart's content. I choose to use VBScript over Javascript because Javascript is horrendously frustrating to me due to inconsistencies in naming standards and and unnecessary case sensitivity. I would rather not receive assistance than have to reead one more sanctimonious lecture about what an idiot I am for using VBScript, OK???
 
Old September 15th, 2008, 03:54 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Your JS function isn't returning any value and yet your VBS code is trying to assign the non-existent return to a variable.

How about trying

<SCRIPT language="VBScript">
MsgBox "Type returned from IsMCS is " & TypeName(IsMCS())
</SCRIPT>

Heck, you've got me curious enough to go try it!
 
Old September 15th, 2008, 03:59 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummm...I was unable to repro this!

Using MSIE 6, I did this:

<html>
<head>
Code:
<script language="VBScript">
Sub Demo( )
    Dim foo
    foo = jsBar( )
    MsgBox "TypeName of foo is " & TypeName(foo)
End Sub
</script>

<script language="JavaScript">
function jsBar( )
{
    alert("jsBar invoked");
}
</script>
</head>
<body>
<form>
<input type=button language="VBScript" onClick="Demo" value="Try me">
</form>
</body>
</html>
And it worked fine. Said that the TypeName was "Empty", which makes sense.

Can you create a simple HTML page that repros what you are seeing???
 
Old September 16th, 2008, 02:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

And if you don't like using JavaScript why do you need to call into a JavaScript function, I can't think of much that can be done by JavaScript that can't be done by VBScript?
For example you can call 'alert' from VBScript, it's not a JavaScript function it's a method of the window object, and in VBScript you could use the MsgBox function which is more powerful.

--

Joe (Microsoft MVP - XML)
 
Old September 16th, 2008, 09:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Old Pedant:

Thanks for your help! This just gets stranger and stranger. The code below WORKS for me:

<html>
    <script language='Javascript'>
        IsMCS("JS CALL");

        function IsMCS(strParm)
        {
            alert("In IsMCS " + strParm);
        }
    </script>

    <script language='VBScript'>
        StatusButtonScript()

        sub StatusButtonScript()
            msgbox "In StatusButtonScript"

        IsMCS("VB CALL")

            msgbox "After function call"
        end sub
    </script>
</html>

producing four msgboxes in this order, as expected:

In IsMCS JS CALL
In Status Button Script
In IsMCS VB Call
After function call

But this code, which just switches the order of the two scripts:

<html>
    <script language='VBScript'>
        StatusButtonScript()

        sub StatusButtonScript()
            msgbox "In StatusButtonScript"

        IsMCS("VB CALL")

            msgbox "After function call"
        end sub
    </script>

    <script language='Javascript'>
        IsMCS("JS CALL");

        function IsMCS(strParm)
        {
            alert("In IsMCS " + strParm);
        }
    </script>
</html>

produces just TWO message boxes:

In StatusButtonScript
In IsMCS JS Call

Neither "In IsMCS VB Call" nor "After function call" appears, so it looks like it's failing in the VB call to IsMCS, but no error is even returned to identify what is happening. The expected "Type Mismatch: IsMCS" has disappeared, but it doesn't look to me like it got past the VB call to IsMCS. If it couldn't find the JS function, I'd have expected it would return the ubiquitous "Object undefined" error, or something equally vague. But it's not indicating any error was encountered - it just doesn't run. Curiouser and curiouser!

In any case, as joefawcett suggested, there is little I can't do in vbscript. In this case, the issue wasn't as simple as displaying a msgbox (and I do know how to use them in vbscript), but rather an httpRequest. I had a functioning javascript version that worked and just wanted to reuse it. But as that didn't want to cooperate in this instance, I figured out how to do it in vbscript instead, as joefawcett suggested. Goodbye uncooperative javascript function!

Thanks for your help!
 
Old September 16th, 2008, 10:27 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can get it to work in the second example by adding "defer" as an attribute to the initial VBScript script tag. Then your boxes will appear but in a different order. The trouble with the example you give is that two different engines are parsing the scripts as it appears and the JavaScript for the IsMCS function hasn't happened when the call is made.

--

Joe (Microsoft MVP - XML)
 
Old September 16th, 2008, 10:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Yeah, I finally figured that out when I started swapping around the functions. This is clearly a poorly defined or little understood issue that only occurs under rare circumstances, as I've been mixing scripts like this for years with no trouble like this one has caused me! I certainly wasn't aware of the defer attribute mentioned, so thanks for pointing that out.

The code is now working...sometimes. It should fire every time the page is refreshed, but it's currently failing to fire the httprequest at apparently random times. So, something's obviously still not right, but it's probably to do with the current phase of the moon or something equally esoteric.
 
Old September 16th, 2008, 03:07 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Oh, it happens this same way in ASP pages.

The order of execution is really weird. Try this code, for example:

Code:
<script runat=server language=vbscript>
Response.Write "VBScript in line #1"
</script>
<script runat=server language=jscript>
Response.Write("JScript in line #2");
</script>
<%
Response.Write "< % code, #3"
%>
<script runat=server language=vbscript>
Response.Write "VBScript in line #4"
</script>
<script runat=server language=jscript>
Response.Write("JScript in line #5");
</script>
<%
Response.Write "< % code, #6"
%>
The output of that is:
JScript in line #2
--------------------------------------------------------------------------------
JScript in line #5
--------------------------------------------------------------------------------
< % code, #3
--------------------------------------------------------------------------------
< % code, #6
--------------------------------------------------------------------------------
VBScript in line #1
--------------------------------------------------------------------------------
VBScript in line #4

In the browser, the only reliable way I have found to make it all work is to use an event handler to fire things off.

e.g., <body onLoad="firstThingYouWantToHaveHappen( )">

Or, as I used in my sample code, an ONCLICK event.
 
Old September 16th, 2008, 03:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Sheeesh! The last time I ran into this sort of problem with was Lotus Notes. And I don't do Notes any more, TYVM!

I usually execute scripts from events, as well. This just happened to be a case where I wanted something to run when the page loaded for a change.

I'll be more careful when mixing scripts in the future! I am still hopeful they'll eventually provide a REAL programming language for the client and script writing by "trial and error" will be a thing of the past. :D
 
Old September 16th, 2008, 03:24 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

ActionScript???

<grin style="evil" />





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to run a .js file from a javascript function sbkumar Javascript How-To 1 July 11th, 2008 11:12 AM
passing empty parameters to a js function crmpicco Javascript How-To 0 September 20th, 2005 10:16 AM
use a JS-variable/function inside a tag cybermaarten Javascript 3 January 20th, 2005 05:42 AM
function in linked js file 'is not defined' John K. King Javascript 4 May 7th, 2004 09:04 PM
Trigger a JS function on loss of focus apd8x Javascript 1 July 14th, 2003 09:00 AM





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