如何使用JavaScript限制每个用户弹出一个弹出窗口

时间:2017-06-06 22:58:39

标签: javascript jquery html css

我找到了我的博客的代码:

<script type="text/javascript">
document.body.onclick= function(){ window.open('My site', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=950, height=650, left = 300, top = 50'); }
</script>

但由于我对JS一无所知,因此我无法对其进行过多编辑。

3 个答案:

答案 0 :(得分:2)

在弹出窗口显示之前检查localStorage项目,当您显示它时,设置localStorage项目。由于这在SO沙箱中不起作用,因此这是一个codepen https://codepen.io/mcoker/pen/PjqQqw

document.body.onclick = function() {
  if (!localStorage.popup) {
    localStorage.popup = 1;
    window.open(
      "My site",
      "poppage",
      "toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=950, height=650, left = 300, top = 50"
    );
  }
};
body {
  min-height: 100vh;
}

答案 1 :(得分:0)

document.body.onclick= function(){ window.open('My site', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=950, height=650, left = 300, top = 50');document.body.onclick=function(){} }

点击一次

后重置onclick属性

答案 2 :(得分:0)

使用Cookie https://jsfiddle.net/arvinddhakad/bq8f241a/

document.body.onclick = function() {
    isPopup = document.cookie.replace(/(?:(?:^|.*;\s*)popup\s*\=\s*([^;]*).*$)|^.*$/, "$1") || false;
    if (!isPopup) {
        window.open('My site', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=950, height=650, left = 300, top = 50');
    document.cookie = "popup=true";
    }
}