动态注入iframe

IFRAME是文档中的文档,或者浮动的框架(FRAME)。iframe元素会创建包含另外一个文档的内联框架

动态注入iframe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// init iframe
var iframe = document.createElement("iframe");

// iframe.setAttribute('src', '');
iframe.setAttribute('name','iframe');
iframe.style.display='none'
document.body.appendChild(iframe);

// inject script
window.frames['iframe'].document.write('<script>var niu = "qiuqiuqiu";console.log(niu);<\/script>')
window.frames['iframe'].document.close();

// get variable
console.log('获取iframe变量:'+window.frames['iframe'].niu);