網站搜尋

在 IE9 中不從 Javascript 傳遞任何東西到 VBScript


我有一個用 VBScript 編寫的框架。在這個框架中的某些函數內,函數的一個參數會在 If 語句中檢查是否為 Nothing,然後執行一些操作。

使用這個框架的程式碼是用 JavaScript 撰寫的。因此,我需要傳遞 Nothing 給函數以執行一些操作。在 Internet Explorer 8 及更早版本中,以下方法可行:

<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>

在 Internet Explorer 9 之前的版本中,輸出將會是:Nothing、Null、Nothing。

在 Internet Explorer 9:無,空,空。

我如何能在 Internet Explorer 9 中把 Nothing 從 JavaScript 傳遞到 VBScript?

這是一個框架函數的例子。我不能更改它,因為它在應用程序中被廣泛使用。

Function ExampleFunction(val)
    If (val Is Nothing) Then
        ExampleFunction = 1
    Else
        ExampleFunction = 0
    End If
End Function

答案#1

不幸的是,你可能被困在這裡——JavaScript 沒有「Nothing」的等價物。更多資訊請參見這篇文章。

不過,以下方法可能有效。在你的 VBScript 代碼中,創建一個名為「GetNothing」的函數,該函數返回「Nothing」。在你的 JavaScript 代碼中,使用「var jsNothing=GetNothing()」。這來自這篇文章。

答案#2

我現在無法使用 Internet Explorer,所以我無法測試這個,但可以嘗試寫一個像這樣的函數:

<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>

當然,我不知道 VBScript 是否允許那樣調用函數……而且你還得處理更多或更少的參數。