Javascript: 当 url 是 localhost 时加载脚本
Javascript: Load script when url is localhost
当 url 是本地主机时,我需要加载文件。
我预计:
if (location.hostname === "localhost" || location.hostname === "127.0.0.1"){
<script src="script-local.js"></script>
} else { //
<script src="https://example.com/script.js"></script>
}
可能吗?
我认为这样的方法可行:
<html>
<body>
<script>
var script = document.createElement("script");
if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
script.src = "http://somedomain.com/somescript.js";
} else {
script.src = "http://somedomain.com/someotherscript.js";
}
document.body.appendChild(script);
</script>
</body>
</html>
当 url 是本地主机时,我需要加载文件。
我预计:
if (location.hostname === "localhost" || location.hostname === "127.0.0.1"){
<script src="script-local.js"></script>
} else { //
<script src="https://example.com/script.js"></script>
}
可能吗?
我认为这样的方法可行:
<html>
<body>
<script>
var script = document.createElement("script");
if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
script.src = "http://somedomain.com/somescript.js";
} else {
script.src = "http://somedomain.com/someotherscript.js";
}
document.body.appendChild(script);
</script>
</body>
</html>