繁体   English   中英

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

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

我正在尝试在Windows 10上发出 Window alert ,但进展并不顺利。

这是我尝试过的:

window.alert("Alert");

alert("Alert");

如果它也是另一种语言也没关系。 但是如果JavaScript中有办法做到,请回答!

alert("Alert"); 应该管用。

你可以在这里测试它: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert

我刚刚做了,在win 10上,它工作正常。

window.alert仅是浏览器 function。 您只能在浏览器环境中执行它。

对于 WSH JScript 中的简单消息框,请编写:

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

如果您需要 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");

您必须将所有内容保存在example.js文件中并通过鼠标双击执行该文件。 如果您需要支持国际消息,请将其保存为unicode格式(但不是UTF-8 格式。)。

一些有用的扩展示例

如果您每 15 秒(或可能 1 小时)需要一些消息框,您可以使用while循环和WScript.sleep function 来完成,因为我们在 WSH JScript 中也没有setTimeout function。

在下一个代码示例中,如果您一直单击“确定”按钮,我们会在无限循环中每 15 秒出现一个消息框。 单击取消按钮将结束整个脚本(每个 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
}

您将在Microsoft Windows Script Host 2.0 开发人员指南在线图书中获得更多信息。

暂无
暂无

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

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