在Javascript文件中定义并调用Wscript函数以在Windows脚本主机中运行
我需要的正是我在问题标题中所问的 我有一个在Javascript文件中定义并调用Wscript函数以在Windows脚本主机中运行,javascript,vbscript,wsh,hta,windows-scripting,Javascript,Vbscript,Wsh,Hta,Windows Scripting,我需要的正是我在问题标题中所问的 我有一个javascript文件,用于计算每年的感恩节日期,提供年份值输入 现在,我希望此javascript可以通过Windows脚本主机执行,并且允许在不更改文件类型或扩展名的情况下创建尽可能多的输入框 我尝试了下面的代码,但它给了我错误预期: ws = WScript.CreateObject('WScript.Shell'); var myDate = new Date(); myDate.setHours(0, 0, 0, 0); // Functi
javascript
文件,用于计算每年的感恩节日期,提供年份值输入
现在,我希望此javascript可以通过Windows脚本主机执行,并且允许在不更改文件类型或扩展名的情况下创建尽可能多的输入框
我尝试了下面的代码,但它给了我错误预期代码>:
ws = WScript.CreateObject('WScript.Shell');
var myDate = new Date();
myDate.setHours(0, 0, 0, 0);
// Function WSHInputBox(Message, Title, Value)
// ' Provides an InputBox function for JScript
// ' Can be called from JScript as:
// ' var result = WSHInputBox("Enter a name", "Input", test);
// WSHInputBox = InputBox(Message, Title, Value)
// End Function
strYear = GetUserInput( "Enter some input:" )
ws.Popup('Year ?'+strYear);
//var strYear = WSHInputBox("Enter the year", "Thanksgiving Year")
myDate.setYear(parseInt(strYear));
// Determine November 1.
myDate.setDate(1);
myDate.setMonth(10);
// Find Thursday.
var thursday = 4;
while(myDate.getDay() != thursday) {
myDate.setDate(myDate.getDate() + 1);
}
// Add 3 weeks.
myDate.setDate(myDate.getDate() + 21);
ws.Popup('Result: ' + myDate);
Function UserInput( myPrompt )
' This function prompts the user for some input.
' When the script runs in CSCRIPT.EXE, StdIn is used,
' otherwise the VBScript InputBox( ) function is used.
' myPrompt is the the text used to prompt the user for input.
' The function returns the input typed either on StdIn or in InputBox( ).
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Check if the script runs in CSCRIPT.EXE
If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
' If so, use StdIn and StdOut
WScript.StdOut.Write myPrompt & " "
UserInput = WScript.StdIn.ReadLine
Else
' If not, use InputBox( )
UserInput = InputBox( myPrompt )
End If
End Function
Function GetUserInput( myPrompt )
' This function uses Internet Explorer to
' create a dialog and prompt for user input.
'
' Version: 2.11
' Last modified: 2013-11-07
'
' Argument: [string] prompt text, e.g. "Please enter your name:"
' Returns: [string] the user input typed in the dialog screen
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Error handling code written by Denis St-Pierre
Dim objIE
' Create an IE object
Set objIE = CreateObject( "InternetExplorer.Application" )
' Specify some of the IE window's settings
objIE.Navigate "about:blank"
objIE.Document.title = "Input required " & String( 100, "." )
objIE.ToolBar = False
objIE.Resizable = False
objIE.StatusBar = False
objIE.Width = 320
objIE.Height = 180
' Center the dialog window on the screen
With objIE.Document.parentWindow.screen
objIE.Left = (.availWidth - objIE.Width ) \ 2
objIE.Top = (.availHeight - objIE.Height) \ 2
End With
' Wait till IE is ready
Do While objIE.Busy
WScript.Sleep 200
Loop
' Insert the HTML code to prompt for user input
objIE.Document.body.innerHTML = "<div align=""center""><p>" & myPrompt _
& "</p>" & vbCrLf _
& "<p><input type=""text"" size=""20"" " _
& "id=""UserInput""></p>" & vbCrLf _
& "<p><input type=""hidden"" id=""OK"" " _
& "name=""OK"" value=""0"">" _
& "<input type=""submit"" value="" OK "" " _
& "OnClick=""VBScript:OK.value=1""></p></div>"
' Hide the scrollbars
objIE.Document.body.style.overflow = "auto"
' Make the window visible
objIE.Visible = True
' Set focus on input field
objIE.Document.all.UserInput.focus
' Wait till the OK button has been clicked
On Error Resume Next
Do While objIE.Document.all.OK.value = 0
WScript.Sleep 200
' Error handling code by Denis St-Pierre
If Err Then ' user clicked red X (or alt-F4) to close IE window
IELogin = Array( "", "" )
objIE.Quit
Set objIE = Nothing
Exit Function
End if
Loop
On Error Goto 0
' Read the user input from the dialog window
GetUserInput = objIE.Document.all.UserInput.value
' Close and release the object
objIE.Quit
Set objIE = Nothing
End Function
WScript.Quit();
ws=WScript.CreateObject('WScript.Shell');
var myDate=新日期();
myDate.setHours(0,0,0,0);
//函数WSHInputBox(消息、标题、值)
//'为JScript提供一个InputBox函数
//'可以从JScript调用为:
//'var result=WSHInputBox(“输入名称”、“输入”和“测试”);
//WSHInputBox=InputBox(消息、标题、值)
//端函数
strYear=GetUserInput(“输入一些输入:”)
ws.Popup('Year?'+strYear);
//var strYear=WSHInputBox(“输入年份”、“感恩节”)
myDate.setYear(parseInt(strYear));
//11月1日确定。
myDate.setDate(1);
myDate.setMonth(10);
//找到星期四。
var=4;
while(myDate.getDay()!=星期四){
myDate.setDate(myDate.getDate()+1);
}
//加上3周。
myDate.setDate(myDate.getDate()+21);
ws.Popup('Result:'+myDate);
函数UserInput(myPrompt)
'此函数提示用户输入一些信息。
'当脚本在CSCRIPT.EXE中运行时,使用StdIn,
'否则将使用VBScript InputBox()函数。
'myPrompt是用于提示用户输入的文本。
'该函数返回在StdIn或InputBox()中键入的输入。
罗布·范德沃德写的
' http://www.robvanderwoude.com
'检查脚本是否在CSCRIPT.EXE中运行
如果UCase(右(WScript.FullName,12))=“\CSCRIPT.EXE”,则
'如果是,请使用标准输入和标准输出
WScript.StdOut.Write myPrompt&“
UserInput=WScript.StdIn.ReadLine
其他的
'如果不是,请使用InputBox()
UserInput=InputBox(myPrompt)
如果结束
端函数
函数GetUserInput(myPrompt)
'此功能使用Internet Explorer来
'创建对话框并提示用户输入。
'
'版本:2.11
'最后修改日期:2013-11-07
'
参数:[字符串]提示文本,例如“请输入您的姓名:”
'返回:[string]在对话框屏幕中键入的用户输入
'
罗布·范德沃德写的
' http://www.robvanderwoude.com
'由Denis St Pierre编写的错误处理代码
昏暗的奥布杰
'创建一个IE对象
Set objIE=CreateObject(“InternetExplorer.Application”)
'指定IE窗口的一些设置
objIE.导航“关于:空白”
objIE.Document.title=“需要输入”和字符串(100“.”)
objIE.ToolBar=False
objIE.resizeable=False
objIE.StatusBar=False
对象宽度=320
物体高度=180
'在屏幕上居中显示对话框窗口
使用objIE.Document.parentWindow.screen
objIE.Left=(.availWidth-objIE.Width)\2
objIE.Top=(.availHeight-objIE.Height)\2
以
“等我准备好了再说
忙的时候去做
WScript.Sleep 200
环
'插入HTML代码以提示用户输入
objIE.Document.body.innerHTML=“”&myPrompt_
&“”&vbCrLf_
&“”&vbCrLf_
&“”_
&“”
'隐藏滚动条
objIE.Document.body.style.overflow=“自动”
“让窗户可见
objIE.Visible=True
'将焦点设置在输入字段上
objIE.Document.all.UserInput.focus
'等待单击“确定”按钮
出错时继续下一步
当objIE.Document.all.OK.value=0时执行
WScript.Sleep 200
“Denis St Pierre的错误处理代码
如果出现错误,则“用户单击红色X(或alt-F4)关闭IE窗口
IELogin=数组(“,”)
objIE,退出
设置对象=无
退出功能
如果结束
环
错误转到0
'从对话框窗口读取用户输入
GetUserInput=objIE.Document.all.UserInput.value
'关闭并释放对象
objIE,退出
设置对象=无
端函数
WScript.Quit();
当我被困在如何在同一个Javascript文件中包含这两种类型的代码时,有人能帮忙吗?我知道有办法,但那是什么问题?问题
- 开发人员VickyDev希望在MSFT JScript文件中运行MSFT VBScript函数
- InputBox是VBScript函数而不是JScript函数
//函数WSHInputBox(消息、标题、值)
//'为JScript提供一个InputBox函数
//'可以从JScript调用为:
//'var result=WSHInputBox(“输入名称”、“输入”和“测试”);
//WSHInputBox=InputBox(消息、标题、值)
//端函数
解决方案
- 使用windows脚本主机文件和脚本标记上的语言声明将VBScript和JScript组合在一起李>
- 确保在声明JScript之前先声明所有VBScript函数
例子
尝试创建.wsf文件。或者使用ScriptControl
或htmlfile
ActiveX执行VBS代码。
// Function WSHInputBox(Message, Title, Value)
// ' Provides an InputBox function for JScript
// ' Can be called from JScript as:
// ' var result = WSHInputBox("Enter a name", "Input", test);
// WSHInputBox = InputBox(Message, Title, Value)
// End Function
<?xml version="1.0"?>
<package>
<job id="uu244trumpehant">
<script language="vbscript">
<![CDATA[
Function HelloWorldVB
msgbox "hello world from VB"
End Function
]]>
</script>
<script language="jscript">
<![CDATA[
HelloWorldJS();
HelloWorldVB();
//HelloWorldVBTwo(); // this one will error
// function defined below
function HelloWorldJS(){
WScript.Echo("hello world from JS");
}
]]>
</script>
<script language="vbscript">
<![CDATA[
Function HelloWorldVBTwo
msgbox "hello world from VBTwo"
End Function
]]>
</script>
</job>
</package>