javascript - 随机 HTML 页面脚本在边缘而不是 chrome 上工作
问题描述
我有一个随机调用与一些 JS 内联的 HTML 页面的脚本。它在 Edge 上运行良好,但在 chrome 上却不行。如何让它在 Chrome 上运行?
试图换个头,但我不知道出了什么问题。
function loadExternalHTMLPage() {
var xmlhttp;
var pagesToDisplay = ['1.html', '2.html'];
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("contentArea").innerHTML = xmlhttp.responseText;
}
}
var randomnumber = Math.floor(Math.random() * pagesToDisplay.length);
xmlhttp.open("GET", pagesToDisplay[randomnumber], true);
xmlhttp.send();
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Random principle</title>
</head>
<script language="javascript" type="text/javascript" src="javascript.js"></script>
<body onload="loadExternalHTMLPage()">
<div id="contentArea">
</div>
</body>
</html>
解决方案
当您使用 XMLHttpRequest 加载文件时,您需要使用一些服务器来托管文件。如果您使用 VS 代码作为代码编辑器,您可以安装 Live 服务器扩展并使用 Live 服务器打开文件。
https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer
推荐阅读
- typescript - 在 Typescript 中解构函数参数
- asp.net-mvc - 如何在 ASP.NET MVC 5 的自定义 html-helper 中使用 Html.Partial()?
- elasticsearch - ElasticSearch 过滤多个必填字段
- spring-batch - 使用 @StepScope 时 Spring Batch 重启功能不起作用
- terminal - Windows 10 更新后,Visual Studio Code 终端以浅蓝色突出显示
- java - 如何修复 64 位/32 位系统的 java.lang.UnsatisfiedLinkError-Problem?
- node.js - MongoNetworkError:无法连接到服务器
- codeigniter - 我已将我的文件从现有服务器迁移到新服务器?但测试,会话正在工作
- html-table - DOMPDF 0.8.3 如何拆分长垂直表
- javascript - JS 文件无法在 CPanel 中加载