我可以通过单击link来从iframe更改父窗口,但是一旦单击链接,iframe也就消失了。有什么方法可以在不关闭iframe的情况下从iframe在后台更改父窗口的路径?
var iframe = document.createElement('iframe');
iframe.onload = function(){
var link=document.createElement("a");
link.appendChild(document.createTextNode("Link"));
link.href = '#';
document.body.appendChild(link);
var s = document.createElement("script");
s.innerHTML = "window.addEventListener('click', function(event) {" +
"window.postMessage('testMsg', '*');" +
"}, false);";
document.head.appendChild(s);
}
iframe.src = 'http://example.org';
document.body.appendChild(iframe);
window.addEventListener('message', function(event) {
window.location.href = "http://jsfiddle.net";}, false );