没有 Web 服务器,无法 link HTML 中的脚本源

Cannot link script source in HTML without web server

在这个link、https://editor.p5js.org/中,有html、javascript和css三个文件。 我在自己的计算机上复制了这些文件,然后 运行 使用 Chrome 浏览器复制了这些文件。它不渲染任何东西。但是,当我 运行 使用网络服务器 Xampp 此页面时,它 运行 成功了。原因是什么?

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/addons/p5.sound.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>
function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
}
html, body {
  margin: 0;
  padding: 0;
}
canvas {
  display: block;
}

将您的 <script> 标签放入 html 的 head 标签中 https://p5js.org/get-started/

喜欢:

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.js"></script>
    <script src="sketch.js"></script>
  </head>
  <body>
    <main>
    </main>
  </body>
</html>

它对我有用 Google Chrome

可能CORS,CORS 通常用于图像和声音文件等类似的东西,但在 google [=18] 中可能有所不同=],你应该 google 如何在 google chrome 中禁用它,看看什么对你有用,我认为它与快捷方式有关,不确定。浏览器还应该在您的控制台中抛出关于 CORS 的废话。

否则你的文件可能在错误的位置,所有的东西都应该在一个文件夹下:

jsProject:
   sketch.js
   index.html
   style.css