有没有一个更简单的(本机也许?)方法,包括一个外部脚本文件在谷歌Chrome浏览器?
目前我是这样做的:
document.head.innerHTML += '<script src="http://example.com/file.js"></script>';
有没有一个更简单的(本机也许?)方法,包括一个外部脚本文件在谷歌Chrome浏览器?
目前我是这样做的:
document.head.innerHTML += '<script src="http://example.com/file.js"></script>';
当前回答
在chrome中,你最好的选择可能是在开发人员工具中的源代码下的snippet选项卡。
它将允许您编写和运行代码,例如,在一个about:空白页面中。
更多信息请访问:https://developers.google.com/web/tools/chrome-devtools/debug/snippets/?hl=en
其他回答
var el = document.createElement("script"),
loaded = false;
el.onload = el.onreadystatechange = function () {
if ((el.readyState && el.readyState !== "complete" && el.readyState !== "loaded") || loaded) {
return false;
}
el.onload = el.onreadystatechange = null;
loaded = true;
// done!
};
el.async = true;
el.src = path;
var hhead = document.getElementsByTagName('head')[0];
hhead.insertBefore(el, hhead.firstChild);
appendChild()是一种更原生的方式:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'script.js';
document.head.appendChild(script);
在chrome中,你最好的选择可能是在开发人员工具中的源代码下的snippet选项卡。
它将允许您编写和运行代码,例如,在一个about:空白页面中。
更多信息请访问:https://developers.google.com/web/tools/chrome-devtools/debug/snippets/?hl=en
你可以获取脚本作为文本,然后求值:
eval(await (await fetch('http://example.com/file.js')).text())
如果你刚开始学习javascript,不想花时间创建一个完整的网页,只是为了嵌入测试脚本,只需在Chrome浏览器的新选项卡中打开开发工具,然后单击控制台。
然后输入一些测试脚本:例如。
console.log('Aha!')
然后在每一行后按Enter(由Chrome提交执行)
或者加载你自己的“外部脚本文件”:
document.createElement('script').src = 'http://example.com/file.js';
然后调用你的函数:
console.log(file.function('驨ꍬ啯ꍲᕤ'))
测试在谷歌Chrome 85.0.4183.121