在模态弹出窗口中打开外部网站

时间:2018-02-28 21:52:51

标签: javascript html css iframe popup

我知道websockets会在新标签页中打开链接 (Chrome和Firefox中的默认行为)

<a href="http://www.example.com" target="_blank">Click here</a>

将其打开in a new window

但是如何在模式弹出窗口中打开外部页面(以屏幕为中心,原始页面的其余部分“变暗”)?

有一个很好的方法吗?

我尝试了以下代码,但它似乎没有工作(点击:弹出窗口打开,重新点击,它关闭,重新点击链接:它不再打开。而且iframe也没有加载)。< / p>

<a href="http://www.example.com" onclick="window.open('http://www.example.com', 
        'newwindow', 'width=700,height=500'); return false;">Click here</a>
document.getElementById("a").onclick = function(e) {
  e.preventDefault();
  document.getElementById("popup").style.display = "block";
  document.getElementById('iframe').src = "http://example.com";
  document.getElementById('page').className = "darken";
  setTimeout(function() { 
    document.getElementById('page').onclick = function() {
      document.getElementById("popup").style.display = "none";
      document.getElementById('page').className = "";
    }
  }, 100);
  return false;
}
#popup { 
  display: none; 
  border: 1px black solid;
  width: 400px; height: 200px; 
  top:20px; left:20px;
  background-color: white;
  z-index: 10;
  padding: 2em;
  position: fixed;
}

.darken { background: rgba(0, 0, 0, 0.7); }

#iframe { border: 0; }

html, body, #page { height: 100%; }

3 个答案:

答案 0 :(得分:1)

根本原因是page.onclick is registered after popup window is open at the first time, then the following 'open popup' will trigger a.onclick and page.onclick in a row, that caused the popup window is open then is closed directly

简单的解决方案是添加一个参数来控制弹出窗口的状态。如果弹出窗口关闭,请不要在page.onclick中执行任何操作。

要删除setTimeout,有两种解决方案:

  1. 添加了另一个参数来控制弹出初始化的状态。

  2. 不要让<div id="page">成为<a id="popup">的父级,因此点击<a id="popup">时不会触发<div id="page">

  3. 以下是两个解决方案:

      

    我更喜欢 Solution2 ,它更好地解耦   解决方案1 ​​,每个组件都有自己的工作。

    解决方案1 ​​:如果弹出窗口已经完成初始化,则添加isInit作为指示。

    PS :在评论中,您提到仅在点击<div id="page">时关闭弹出窗口,要在解决方案1中实现此功能,我认为您必须绑定事件= {{1} },mouseout判断鼠标点击的位置。

    mouseenter
    document.getElementById("a").onclick = function(e) {
      e.preventDefault();
      var isInit = true; // indicates if the popup already been initialized.
      var isClosed = false; // indicates the state of the popup
      document.getElementById("popup").style.display = "block";
      document.getElementById('iframe').src = "http://example.com";
      document.getElementById('page').className = "darken";
      document.getElementById('page').onclick = function() {
        if(isInit){isInit=false;return;}
        if(isClosed){return;} //if the popup is closed, do nothing.
        document.getElementById("popup").style.display = "none";
        document.getElementById('page').className = "";
        isClosed=true;
      }
      return false;
    }
    #popup { 
      display: none; 
      border: 1px black solid;
      width: 400px; height: 200px; 
      top:20px; left:20px;
      background-color: white;
      z-index: 10;
      padding: 2em;
      position: fixed;
    }
    
    .darken { background: rgba(0, 0, 0, 0.7); }
    
    #iframe { border: 0; }
    
    html, body, #page { height: 100%; }

    解决方案2 :make <div id="page"> <a href="" id="a">Click me</a><br> Hello<br> Hello<br> <div id="popup"> External website: <iframe id="iframe"></iframe> </div> </div><div id="page">是堂兄而不是父母关系。

    <a id="popup">
    document.getElementById("popup").showpopup = function() {
      document.getElementById("popup").style.display = "block";
      document.getElementById('iframe').src = "http://example.com";
      document.getElementById('page').className = "darken";
      document.getElementById("page").style.display = "block";
    }
    
    document.getElementById("a").onclick = function(e) {
      e.preventDefault();
      document.getElementById("popup").showpopup();
    }
    
    document.getElementById('page').onclick = function() {
      if(document.getElementById("popup").style.display == "block") {
        document.getElementById("popup").style.display = "none";
        document.getElementById("page").style.display = "none";
        document.getElementById('page').className = "";
      }
    };
    #popup { 
      display: none; 
      border: 1px black solid;
      width: 400px; height: 200px; 
      top:20px; left:20px;
      background-color: white;
      z-index: 10;
      padding: 2em;
      position: fixed;
    }
    
    #page { 
      display: none; 
      width: 100%; height: 100%; 
      top:0px; left:0px;
      z-index: 9;
      padding: 2em;
      position: absolute;
    }
    
    .darken { background: rgba(0, 0, 0, 0.7); }
    
    #iframe { border: 0; }
    
    html, body, #page { height: 100%; }

答案 1 :(得分:1)

基于Sphinx的优秀答案,我将使用以下内容创建一个模式弹出窗口,在iframe中显示一个外部网站,其背景为深色背景弹出窗口是打开的:

document.getElementById("link").onclick = function(e) {
  e.preventDefault();
  document.getElementById("popupdarkbg").style.display = "block";
  document.getElementById("popup").style.display = "block";
  document.getElementById('popupiframe').src = "http://example.com";
  document.getElementById('popupdarkbg').onclick = function() {
      document.getElementById("popup").style.display = "none";
      document.getElementById("popupdarkbg").style.display = "none";
  };
  return false;
}

window.onkeydown = function(e) {
    if (e.keyCode == 27) {
      document.getElementById("popup").style.display = "none";
      document.getElementById("popupdarkbg").style.display = "none";
      e.preventDefault();
      return;
    }
}
#popup { display: none; position: fixed; top: 12%; left: 15%; width: 70%; height: 70%; background-color: white; z-index: 10; }
#popup iframe { width: 100%; height: 100%; border: 0; }
#popupdarkbg { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; background-color: rgba(0,0,0,.75); display: none; }
<div id="main">
<a href="" id="link">Click me</a><br>
</div>

<div id="popup"><iframe id="popupiframe"></iframe></div>
<div id="popupdarkbg"></div>

答案 2 :(得分:0)

document.getElementById("a").onclick = function(e) {
  e.preventDefault();
  var isInit = true; // indicates if the popup already been initialized.
  var isClosed = false; // indicates the state of the popup
  document.getElementById("popup").style.display = "block";
  document.getElementById('iframe').src = "http://example.com";
  document.getElementById('page').className = "darken";
  document.getElementById('page').onclick = function() {
    if(isInit){isInit=false;return;}
    if(isClosed){return;} //if the popup is closed, do nothing.
    document.getElementById("popup").style.display = "none";
    document.getElementById('page').className = "";
    isClosed=true;
  }
  return false;
}
#popup { 
  display: none; 
  border: 1px black solid;
  width: 400px; height: 200px; 
  top:20px; left:20px;
  background-color: white;
  z-index: 10;
  padding: 2em;
  position: fixed;
}

.darken { background: rgba(0, 0, 0, 0.7); }

#iframe { border: 0; }

html, body, #page { height: 100%; }
<div id="page">
  <a href="" id="a">Click me</a><br>
  Hello<br>
  Hello<br>

  <div id="popup"> 
  External website:
  <iframe id="iframe"> height=“200”</iframe>
  </div>

</div>