简体   繁体   English

如何在 Windows 上使用 JScript 制作警报/消息/弹出框?

[英]How do you do an alert / message / popup box using JScript on Windows?

I'm trying to do a Window alert on Windows 10 and it's not going so well.我正在尝试在Windows 10上发出 Window alert ,但进展并不顺利。

This is what I tried:这是我尝试过的:

window.alert("Alert");

And

alert("Alert");

It doesn't matter if its in another language too.如果它也是另一种语言也没关系。 But if there is a way to do it in JavaScript, please answer!但是如果JavaScript中有办法做到,请回答!

alert("Alert"); should work.应该管用。

You can test it out here: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert你可以在这里测试它: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert

I just did, on win 10 and it works fine.我刚刚做了,在win 10上,它工作正常。

window.alert is the browser function only. window.alert仅是浏览器 function。 You can execute it in browser environment only.您只能在浏览器环境中执行它。

For simple message box in WSH JScript write:对于 WSH JScript 中的简单消息框,请编写:

WScript.Echo("Windows message box sample");

If you need extended message box in WSH JScript write:如果您需要 WSH JScript 中的扩展消息框,请编写:

var mbOK = 0,
    mbOKCancel = 1,
    mbCancel = 2,
    mbInformation = 64, // Information icon
    text  = //"Windows Script Host sample",
    title = //"Title sample",
    wshShell = WScript.CreateObject("WScript.Shell"),
    intDoIt =  wshShell.Popup(text,
                    0, //if > 0, seconds to show popup. With 0 it is without timeout.
                    title,
                    mbOKCancel + mbInformation);
if(intDoIt == mbCancel) 
{
    WScript.Quit(); //End of WScript (every for & while loops end too)
}

WScript.Echo("Sample executed");

You have to save all it in example.js file and execute this file with mouse double click.您必须将所有内容保存在example.js文件中并通过鼠标双击执行该文件。 If you need support for internationale messages then save it in unicode format (but NOT in UTF-8 format.).如果您需要支持国际消息,请将其保存为unicode格式(但不是UTF-8 格式。)。

Some useful exended example一些有用的扩展示例

If you need some message box every 15 seconds (or maybe 1 hour) you can do it with while loop and WScript.sleep function, because we don't have setTimeout function in WSH JScript too.如果您每 15 秒(或可能 1 小时)需要一些消息框,您可以使用while循环和WScript.sleep function 来完成,因为我们在 WSH JScript 中也没有setTimeout function。

In the next code sample we have a message box every 15 seconds in endless loop if you click all the time on OK button.在下一个代码示例中,如果您一直单击“确定”按钮,我们会在无限循环中每 15 秒出现一个消息框。 A click on cancel button will end the full script (every for & while loops end too).单击取消按钮将结束整个脚本(每个 for & while 循环也结束)。

while(true)
{
    var mbOK = 0,
        mbOKCancel = 1,
        mbCancel = 2,
        mbInformation = 64, // Information icon
        text  = "TO-DO: Write your App code! Do It! Just Do It!",
        title = "Simple TO-DO thing",
        wshShell = WScript.CreateObject("WScript.Shell"),
        intDoIt =  wshShell.Popup(text,
                        0, //if > 0, seconds to show popup. With 0 it is without timeout.
                        title,
                        mbOKCancel + mbInformation);
    if(intDoIt == mbCancel) 
    {
        WScript.Quit(); //End of WScript (every for & while loops end too)
    }

    WScript.sleep(15000); //Every 15 seconds. And 60*60*1000=3600000 for every hour
}

More information you will get in Microsoft Windows Script Host 2.0 Developer's Guide online book.您将在Microsoft Windows Script Host 2.0 开发人员指南在线图书中获得更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM