使用:
1、在项目里面新建index.js,将下面代码黏贴进去
export const createScript = (result,type,pos) => {
var positions = pos? document.getElementsByTagName('head')[0] :document.getElementsByTagName('body')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
if (type === 'url') {
script.src = result;
} else {
script.innerHTML = result;
}
positions.appendChild(script);
}
2、通过import { createScript } from './index';
3、调用
let scriptCode = `
function fns(){
alert("原生js动态注入脚本内容")
};
fns();
createScript(scriptCode)
网友评论