简体   繁体   English

使用按钮关闭iframe模式

[英]Close iframe modal with button

I am using the following to load an iframe from a bookmarklet: 我正在使用以下内容从小书签加载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));

and to close it I am trying: 并关闭它,我正在尝试:

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

I've also tried: 我也尝试过:

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

But they are not working, they do nothing. 但是他们没有工作,他们什么也没做。

I know I can use: 我知道我可以使用:

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

But this then causes a the browser to re-refresh the page they were looking at and also the user has to press the back button twice to get to the page before which is not ideal. 但这然后导致浏览器重新刷新他们正在查看的页面,并且用户还必须按两次后退按钮才能进入该页面,在此之前并不理想。

Any ideas? 有任何想法吗?

you should add some name or id to your iframe 您应该在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));

And then 接着

<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>

EDIT working solution, have just tested on stackoverflow inside Chrome JS console: EDIT工作解决方案,刚刚在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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM