簡體   English   中英

在Javascript文件中定義並調用Wscript函數以在Windows Script Host中運行

[英]Define and call Wscript function inside Javascript file to run in Windows Script Host

我需要完全像我問題標題。

我有一個用於計算每年感恩節日期的javascript文件,只要輸入年份值即可。

現在我希望這個javascript可以由Windows腳本宿主執行,並且還允許創建盡可能多的InputBox,而無需更改文件類型或擴展名。

我嘗試下面的代碼,但它給了我錯誤expected ;

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();

有人可以幫忙,因為我被困在如何在這個相同的Javascript文件中包含兩種類型的代碼,我知道有一些方法,但那是什么?

問題

  • 開發人員VickyDev想要在MSFT JScript文件中運行MSFT VBScript函數
  • InputBox是VBScript函數,而不是JScript函數
// 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

  • 使用Windows腳本宿主文件和腳本標記上的語言聲明將VBScript和JScript結合在一起。
  • 確保在聲明JScript之前首先聲明所有VBScript函數

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM