Bug*_*nny 53
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.onload = function() {
callFunctionFromScript();
}
script.src = 'path/to/your-script.js';
head.appendChild(script);
Run Code Online (Sandbox Code Playgroud)
这是我如何在没有任何源文件等的情况下动态注入函数的方法。
document.head.appendChild(document.createElement('script').text = 'function LogIt(msg) { console.log(msg);}' );
Run Code Online (Sandbox Code Playgroud)
并注射到体内
document.body.appendChild(document.createElement('script').text = 'function LogIt(msg) { console.log(msg);}' );
Run Code Online (Sandbox Code Playgroud)
执行此操作后,如果您尝试LogIt('hello');,您应该在控制台中看到“hello”。
| 归档时间: |
|
| 查看次数: |
56029 次 |
| 最近记录: |