纯 Javascript 弹出/模式离开站点同意

Pure Javascript Popup / Modal Leaving Site Consent

我尝试创建简单的弹出窗口并征得网站同意。

演示:See demo on codepen

function exModal() {
  const modalhtml=` <div class="modal-header">
  &#128279; You're Leaving Our Site!
   <span class="close-modal">&times</span>
   </div> 
   <div class="modal-body">
   This is a link to an external site. Click OK to continue to the content. Feel free to comeback again. Make Sure To Follow Instructions Properly.
   <br/><br/>
   <div class="show-link"></div>
   </div> 
   <div class="modal-footer">
   <button class="close-modal">Close</button>
   <button class="confirm-modal">OK</> 
   </div> `;

  const getlink = document.querySelector('.ex-link').getAttribute('data-href');
  const extra=document.createElement('div');
  extra.classList.add('modal-content');
  extra.innerHTML=modalhtml;
  document.body.appendChild(extra);
  document.querySelector('.show-link').innerHTML='('+getlink+')';
  const close=document.querySelectorAll('.close-modal');
  document.querySelector('.confirm-modal').addEventListener('click', (e) => {
    window.open(getlink, '_blank');
  });
  let i;
    for (i = 0; i < close.length; i++) {
        close[i].addEventListener('click', (e) => {
        extra.remove();
    });
  }
}

问题

  1. 打开模式时如何防止紫色按钮被点击。
  2. 我还想添加功能,比如当有人点击确定按钮时,它会在其他选项卡中打开 link 时关闭模式。
  3. 我可以用一个事件侦听器处理两个事件吗? (参考(2)问题)那怎么办?

一个好的参考资料就可以了。我试过谷歌搜索,但大多数教程包括 jquery 或其他脚本

这是我所做的:

  1. 我换了
<a class="ex-link" data-href="https://www.techlegionbd.com" onclick="exModal()">Tech Legion BD</a>

<button class="ex-link" type="submit">Tech Legion BD</button>
  1. 在CSS我替换了
a.ex-link {
...
}

button.ex-link {
  1. 最后我使用函数式方法重写了脚本:

CSS

 button.ex-link {
 width: 150px;
 display: block;
 margin: 10px auto;
 padding: 20px;
 border: 1px solid white;
 background-color: #6600d6;
 color: white;
 font-family: 'Martel Sans', sans-serif;
 font-weight: 800;
 text-align: center
}

.modal-content {
 position: fixed;
 top: 30%;
 left: 50%;
 margin-left: -190px;
 max-width: 380px;
 border: 2px dotted #008080;

}

.modal-header {
 font-size: 20px;
 padding: 15px;
 border-bottom: 1px solid #cecece;
 background: #edfdfe;
}

.modal-body {
 padding: 20px;
 text-align: center;
 border-bottom: 1px solid #cecece;
}

.modal-footer {
 padding: 10px;
 background: #edfdfe;
}

.modal-footer>button {
 padding: 6px 10px;
 margin-left: 5px;
 cursor: pointer;
 width: 50px;
 background: #75e4d5;
 border: 1px solid #008080;
 border-radius: 4px;
}

.modal-header>.close-modal {
 position: absolute;
 right: 0;
 top: 0;
 padding: 15px;
 cursor: pointer;
}

HTML

<!-- <a class="ex-link" data-href="https://www.techlegionbd.com" onclick="exModal()">Tech Legion BD</a>-->
<button class="ex-link" type="submit">Tech Legion BD</button>

JAVASCRIPT

const openModalButton = document.querySelector("button");
const extra = document.createElement('div');
const getlink = "https://www.techlegionbd.com";

const modalhtml = ` <div class="modal-header">
 &#128279; You're Leaving Our Site!
<!-- <span class="close-modal">&times</span>-->
</div>
 <span class="close-modal" onclick="closeModal()">&times</span>
</div>
<div class="modal-body">
 This is a link to an external site. Click OK to continue to the content. Feel free to comeback again. Make Sure To Follow Instructions Properly.
 <br /><br />
 <div class="show-link"></div>
</div>
<div class="modal-footer">
 <!--<button class="close-modal">Close</button>-->
 <button class="close-modal" onclick="closeModal()">Close</button>


 <button class="confirm-modal">OK</>
</div> `;


function openModal() {
 //refewnce variables 
 extra.classList.add('modal-content');
 extra.innerHTML = modalhtml;
 document.body.appendChild(extra);
 document.querySelector('.confirm-modal').addEventListener('click', (e) => {
  window.open(getlink, '_blank');
 });

}
openModalButton.addEventListener("click", () => { openModal() })



function closeModal() {
 document.body.removeChild(extra);
}