125
votes

I have a framework written in VBScript. Inside some function in this framework parameter of the function is checked for Nothing in If statement and then some actions executed. Code that uses framework written in Javascript. So I need to pass Nothing to function to perform some actions. In IE8 and earlier versions worked next approach:

<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 IE < 9 output will: Nothing, Null, Nothing.

In IE9: Nothing, Null, Null.

How can I pass Nothing from Javascript to VBScript in IE9?

Sorry, I know it's ugly, but I'm trapped. And hate VBScript.

edit: There is an example of 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

Update

Quit job. Found a better one.

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.Cheran Shunmugavel
I closed this out as too localized for you. In the future, you can just flag to let us know :)Tim Post♦
Does VBScript have And Also? I know VB.Net has it. As lame as that is, at least it will allow for short-circuiting.vbullinger
@vbullinger: No, it has not lazy logic. blogs.msdn.com/b/ericlippert/archive/2004/07/15/184431.aspx -Eric Lippert's article.mixel

4 Answers

17
votes

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

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

4
votes

This question is fascinating, I thought I'd try and answer it just for the fun of it.

(Congrats to mixel on getting a better job!)

I don't have access to IE right now, so I can't test this, but what if you tried writing 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 VB script allows calling functions like that... and you'd have to deal with more/fewer arguments.

0
votes

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

0
votes

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

<HTML>
<HEAD>
<meta http-equiv="x-ua-compatible" content="IE=10">
<TITLE>Pass Javscript 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>