user2890391
user2890391

Reputation:

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.

Upvotes: 1

Views: 2005

Answers (3)

Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 66971

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

  };

Upvotes: 0

Corey Rothwell
Corey Rothwell

Reputation: 394

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++;
}

Upvotes: 1

epascarello
epascarello

Reputation: 207511

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.

Upvotes: 1

Related Questions