预热脚本 JavaScript

Warm Up script JavaScript

本文关键字:JavaScript 脚本      更新时间:2023-09-26

我找到了一个脚本并进行了许多更改,包括放置在HTML中以通过网页运行脚本。在网页内,如何在不使用提示等的情况下运行脚本。我确定我做错了什么根本上的事情。

<!DOCTYPE HTML>
<html>
<head>
<title>Volume</title>
</head>
<body>
<h1>Warmup Script</h1>
<script>
function warmup() {
    warmUpSite("http://TestWebSite.com");
}
function warmUpSite(url) {
    console.info("warming up: " + url);
    var req = require('request');
    req.get({ url: url }, function(error, response, body) {
        if (!error) {
            alert("hot hot hot! " + url);
        } else {
            alert('error warming up ' + url + ': ' + error);
        }
    });
}
</script>
</body>
</html>

在脚本底部添加对 warmup() 函数的调用:

warmup();
</script>
</body>
</html>

这样调用warmup函数:

<!DOCTYPE HTML>
<html>
<head>
<title>Volume</title>
</head>
<body>
<h1>Warmup Script</h1>
<script>
function warmup() {
    warmUpSite("http://TestWebSite.com");
}
function warmUpSite(url) {
    console.info("warming up: " + url);
    var req = require('request');
    req.get({ url: url }, function(error, response, body) {
        if (!error) {
            alert("hot hot hot! " + url);
        } else {
            alert('error warming up ' + url + ': ' + error);
        }
    });
}
// invoke function
warmup();
</script>
</body>
</html>

看起来您正在尝试尝试使用 require 调用加载模块。如果您运行此代码,您将在控制台中看到(在 Chrome 中按 F12):Uncaught ReferenceError: require is not defined

这是因为 require 函数不是内置在 JavaScript 中的。您需要为此加载一个库。

也许您正在尝试使用require.js?