从html页面获取js(node.js)代码并在加载后运行它

时间:2018-04-07 20:05:13

标签: javascript html

我试图从其他地方获取js代码并运行它 但它在加载后没有运行

有谁知道怎么跑? (代码在这里)

感谢

<html>
<head>
</head>
<body>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.src = 'http://localhost/check.js';
    document.getElementsByTagName('head')[0].appendChild(script);
}
</script>

</body>
</html>

js page:

alert("work?");

1 个答案:

答案 0 :(得分:0)

你创建了http服务器,哪个监听80端口? localhost使用if本地服务器,如果要加载本地文件 - 使用文件路径(不是服务器localhost)。