【发布时间】:2019-02-22 11:07:56
【问题描述】:
我的索引文件中有一个 iframe,我想从 iframe 中获取所有 html 并将其存储在一个变量中。 iframe 中加载的内容来自另一个域。所以我正在尝试使用 javascript 和 postMessage 以及 eventListener 来实现这一点。到目前为止,这是我的代码。我有一个调用 click 方法的按钮。
index.vue
click() {
let iframe = document.getElementById('the_iframe');
iframe.contentWindow.postMessage('Dont really know what to send here', '*');
this.test();
},
test() {
window.addEventListener('message', function(event) {
//Just a test, as the it wont go in the else
console.log(event.currentTarget.document);
if (event.origin !== 'http://testdomain') {
console.log('ERROR');
} else {
console.log(event.currentTarget.document);
const html = event.currentTarget.document);
//This however doesnt work. The value is not stored in the const
console.log(event.currentTarget.document);
}
});
}
当我在控制台记录 event.currentTarget.document 时,它会打印出站点的整个 html 结构,包括父页面(索引)。我的问题是:如何获取索引文件中 iframe 的所有 html?以及如何将其存储在变量中?
【问题讨论】:
标签: javascript html iframe