Pass Nothing from Javascript to VBScript in IE9
Asked Answered
C

4

127

I have a framework written in VBScript. Inside some function in this framework, a parameter of the function is checked for Nothing in an If statement and then some actions are executed.

Code that uses the framework is written in JavaScript. So I need to pass Nothing to the function to perform some actions. In Internet Explorer 8 and earlier versions, the following approach worked:

<script type="text/vbscript">
    Function Test(val)
        If (IsNull(val)) Then
            Test = "Null"
        ElseIf (IsObject(val)) Then
            If (val Is Nothing) Then
                Test = "Nothing"
            End If
        End If
    End Function

    Dim jsNothing
    Set jsNothing = Nothing
    msgBox(Test(jsNothing))
    msgBox(Test(Null))
</script>


<script type="text/javascript">
    alert(Test(jsNothing));
</script>

In Internet Explorer before version 9, the output will: Nothing, Null, Nothing.

In Internet Explorer 9: Nothing, Null, Null.

How can I pass Nothing from JavaScript to VBScript in Internet Explorer 9?

There is an example of a framework function. I can not change it, because it is widely used in application.

Function ExampleFunction(val)
    If (val Is Nothing) Then
        ExampleFunction = 1
    Else
        ExampleFunction = 0
    End If
End Function
Conspire answered 27/9, 2011 at 11:38 Comment(4)
Small sidenote: VBScript doesn't have short-circuited logical operators, so the statement IsObject(val) And val Is Nothing will still result in an error. You'll have to split it into a nested If.Parthinia
I closed this out as too localized for you. In the future, you can just flag to let us know :)Rhodic
Does VBScript have And Also? I know VB.Net has it. As lame as that is, at least it will allow for short-circuiting.Yumuk
@vbullinger: No, it has not lazy logic. blogs.msdn.com/b/ericlippert/archive/2004/07/15/184431.aspx -Eric Lippert's article.Conspire
E
18

Unfortunately, you are probably stuck here - JavaScript does not have a "Nothing" equivalent. See this article for more information.

However, the following may work. In your VBScript code, create a function called "GetNothing" that returns "Nothing". In your JavaScript code, use "var jsNothing = GetNothing()". It comes from this article.

Eastertide answered 16/2, 2012 at 16:37 Comment(2)
Both links are broken. The first one redirects to a generic page and for the second "Hmm. We’re having trouble finding that site. We can’t connect to the server at www.siteexperts.com." is reported.Indiana
OK, the OP has left the building ("Last seen more than 2 years ago")Indiana
M
4

I don't have access to Internet Explorer right now, so I can't test this, but try to write a function like this:

<script type="text/vbscript">
  Function CallWithNulls(fn, arg1, arg2, arg3)
    If (isNull(arg1)) arg1 = Nothing
    If (isNull(arg2)) arg2 = Nothing
    If (isNull(arg3)) arg3 = Nothing
    fn(arg1, arg2, arg3)
  End Function
  Function IsNothing(arg1, arg2, arg3)
     return arg1 is Nothing
  End Function
</script>

<script type="text/javascript">
  alert(CallWithNulls(IsNothing, null, 1, 2));
</script>

Of course, I don't know if VBScript allows calling functions like that... and you'd have to deal with more/fewer arguments.

Managua answered 24/10, 2014 at 17:0 Comment(3)
It's a nice workaround. But this question is already 3 years dead. And I wish VBScript to be dead too :-) Thank's for effort and catch the upvote.Conspire
I wasn't trying to fix the problem (since I saw you'd already found a better solution), it was just an interesting question and I thought I'd give it a stab. Thanks for the upvote!Managua
VBScript continues to be the scripting engine that powers some of HP's products, so it indeed should not die. Yet.Sociolinguistics
C
0

Use a value such as zero or even a negative number. That would allow for you to simply use falsy evaluations, and then you don't have to worry about different browsers and their quirks in evaluating the NULL object.

Coraleecoralie answered 28/9, 2011 at 6:56 Comment(1)
Thanks for an answer but problem is in framework. It was written long ago and is used widely in large application and I can not change just it. I added to question example of a function of framework that called from javascript.Conspire
H
0

As an example, something like this will work, but if the browser is Internet Explorer 11 or later you will need the 'meta' tag.

<html>

    <head>
        <meta http-equiv="x-ua-compatible" content="IE=10">
        <title>Pass JavaScript to VBScript</title>

        <script>
            var val = "null";

            window.alert("Test: " +  val);
        </script>

        <script type="text/vbscript">

            PassNothing(val)

            Sub PassNothing(value)
                If LCase(value) = "null" Then
                    MsgBox "Java passed 'null' so VBScript = 'Nothing'"
                Else
                    Msgbox "Nothing received"
                End If
            End Sub

        </script>

    </head>

</html>
Harlequin answered 1/4, 2019 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.