
使用:
1、在項(xiàng)目里面新建index.js,將下面代碼黏貼進(jìn)去
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、調(diào)用
let scriptCode = `
? ? ? ? ? ? function fns(){
? ? ? ? ? ? ? ? alert("原生js動(dòng)態(tài)注入腳本內(nèi)容")
? ? ? ? ? ? };
? ? ? ? ? ? fns();
createScript(scriptCode)? ? ? ? ??
4、預(yù)覽效果
