如何让javascript在EC2中的index.html文件上运行



我已经启动了一个EC2实例。在用户数据中,我有以下脚本,但我的javascript不会运行。我只看到文本。我该怎么办?我在本地运行了这个程序,并在浏览器中按预期工作。

#!/bin/bash
# get admin privileges
sudo su
# install httpd (Linux 2 version)
yum update -y
yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service
echo "<body>
Text here
<br>
<p id="quote"></p>
<script>
var affirmArray = [
"text here", 
"text here", 
"text here", 
"text here"
];
var rand = Math.floor(Math.random() * affirmArray.length);
function showquote(){
document.getElementById("quote").innerHTML = affirmArray[rand];
}
showquote();
</script>
</body>" > /var/www/html/index.html

您没有链接到服务器上javascript库的路径或主机,您需要在引用javascript函数之前执行此操作。

自己下载并托管它,或者获得一个javascript库的缩小版本的链接。

在运行当前获得的脚本之前,请先包含这样的内容。

<script>file path/ or http://linkToMinifiedVersion</script>  

最新更新