在 IE9 中不将任何内容从 Javascript 传递到 VBScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7568471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass Nothing from Javascript to VBScript in IE9
提问by mixel
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:
我有一个用 VBScript 编写的框架。在此框架中的某些函数内部检查函数的参数在 If 语句中是否为 Nothing,然后执行一些操作。使用用 Javascript 编写的框架的代码。所以我需要传递 Nothing 来执行一些操作。在 IE8 和更早版本中,下一个方法是:
<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.
在 IE < 9 中输出将:Nothing、Null、Nothing。
In IE9: Nothing, Null, Null.
在 IE9 中:无,空,空。
How can I pass Nothing from Javascript to VBScript in IE9?
如何在 IE9 中将任何内容从 Javascript 传递到 VBScript?
Sorry, I know it's ugly, but I'm trapped. And hate VBScript.
对不起,我知道这很丑,但我被困住了。并且讨厌 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
更新
Quitted job. Found a better one.
辞职了。找到了一个更好的。
回答by Pandelon
Unfortunately, you are probably stuck here - JavaScript does not have a "Nothing" equivalent. See This Articlefor more information.
不幸的是,您可能被困在这里 - JavaScript 没有“Nothing”等价物。有关更多信息,请参阅本文。
[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
[编辑] 但是,以下可能有效。在您的 VBScript 中创建一个名为“GetNothing”的函数,该函数返回“Nothing”。在您的 JavaScript 中使用“var jsNothing = GetNothing()”。来自这篇文章
回答by cwohlman
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!)
(恭喜 mixel 找到了更好的工作!)
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:
我现在无法访问 IE,因此我无法对此进行测试,但是如果您尝试编写这样的函数会怎样:
<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.
当然,我不知道 VB 脚本是否允许调用这样的函数……而且您必须处理更多/更少的参数。
回答by svstackoverflow
As an example, something like this will work but if the browser is IE11 or later you will need the 'meta' tag.
举个例子,像这样的东西可以工作,但如果浏览器是 IE11 或更高版本,你将需要“元”标签。
<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>
回答by Timothy Allyn Drake
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.
使用诸如零甚至负数之类的值,这样您就可以简单地使用虚假评估,然后您就不必担心不同的浏览器及其在评估NULL
对象时的怪癖。