简体   繁体   中英

JavaScript - Pop-Up Window opening only ONCE

Need to create a javascript that opens a single window.

Code:

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

What should I do? This script opens every single click on the website - I want it only once.

Add a flag that says you opened it. Check the flag, if set, than do not open it.

If it is only one time on the entire site, than means cookie or localstorage.

var count = 0;

document.body.onclick= function(){

  if(count === 0) window.open('www.androidhackz.blogspot.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
  count++;
}
  var clickedAlready = false;      

  document.body.onclick = function() {

     if (!clickedAlready) {
         window.open('www.androidhackz.blogspot.com', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
         clickedAlready = true;
     }

  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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