0

我正在使用以下内容从书签加载 iframe:

javascript:(function(d){
var%20modal=document.createElement('iframe');
modal.setAttribute('src','http://myurl.com/page.html?url=
'+encodeURIComponent(window.location.href)+'&
page_title='+document.title);
modal.setAttribute('scrolling','no');
modal.className='modal';
document.body.appendChild(modal);
var c=document.createElement('link');
c.type='text/css';
c.rel='stylesheet';
c.href='//myurl.com/css/wl_iframe.css';
document.body.appendChild(c);
}(document));

并关闭它我正在尝试:

<button onclick="parent.$.iframe.close();">X Close Window</button>

我也试过:

<button onclick="modal.$.iframe.close();">X Close Window</button>

但他们没有工作,他们什么也不做。

我知道我可以使用:

<a href="underneath url" target="_parent">X Close Window</a>

但这会导致浏览器重新刷新他们正在查看的页面,并且用户必须按两次后退按钮才能到达之前的页面,这并不理想。

有任何想法吗?

4

1 回答 1

0

您应该在 iframe 中添加一些名称或 ID

javascript:(function(d){
var modal=document.createElement('iframe');
modal.setAttribute('src','http://myurl.com/page.html?url='+encodeURIComponent(window.location.href)+'&page_title='+document.title);
modal.setAttribute('scrolling','no');

modal.setAttribute('name', 'my_modal_window');

modal.className='modal';
document.body.appendChild(modal);
var c=document.createElement('link');
c.type='text/css';
c.rel='stylesheet';
c.href='//myurl.com/css/wl_iframe.css';
document.body.appendChild(c);
}(document));

接着

<a href="#" onclick="var modals = document.getElementsByName('my_modal_window'), i = modals.length; while(i--){ modals[i].parentNode.removeChild(modals[i])}">X close window</a>

编辑 工作解决方案,刚刚在 Chrome JS 控制台中的 stackoverflow 上进行了测试:

(function(d){
var modal=document.createElement('iframe');
modal.setAttribute('src','http://myurl.com/page.html?url='+encodeURIComponent(window.location.href)+'&page_title='+document.title);
modal.setAttribute('scrolling','no');

modal.setAttribute('name', 'my_modal_window');

modal.className='modal';
document.body.appendChild(modal);
var c=document.createElement('link');
c.type='text/css';
c.rel='stylesheet';
c.href='/css/wl_iframe.css';
document.body.appendChild(c);

var a = document.createElement('a');
a.onclick= function(){
   var modals = document.getElementsByName('my_modal_window'),i = modals.length;
   while (i--) {
      modals[i].parentNode.removeChild(modals[i]);
   }
};
a.innerHTML = 'Close Iframes';
document.body.appendChild(a);
}(document))
于 2012-08-17T08:46:54.893 回答