Javascript popup only once for one user/session

Posted on 2006-05-26
Last Modified: 2008-03-06
I have a 5 page web application (i.e a series of pages the user can go to by clicking "next", or "previous" buttons) to enter input and get the output. I would like to get some user feedback about the web application by having a survey window popup while user is accessing the application. The window should not open twice in the same session for the user.

I have the code (which may be set up in 1 or more pages) to popup the window when a random number equals another. But the problem with this is that multiple popup windows might open for the same user. There is no way to keep track of who the user is.

<script language="JavaScript">

var rndnumber1 = Math.random();
var rndnumber2 = Math.random();

rndnumber1 = Math.floor(rndnumber1*10000);
rndnumber2 = Math.floor(rndnumber2*10000);

rndnumber1 = rndnumber1 % 10;
rndnumber2 = rndnumber2 % 10;

if (rndnumber1 == rndnumber2) {



Thanks for any help. I am open to any other ideas.

Question by:momer123
    LVL 9

    Expert Comment

    One way is to use a cookie. Keep track in the cookie that you popped it up. You can make the cookie persistent (stays for as long as you want) or session based which lasts for the lenght of the browser session. If they close the browser, the browser session cookie would go away.

    Save the cookie in the pop-up window and then check the cookie in other pages and if not there and it satisfies your existing random check, pop up the window.
    LVL 5

    Accepted Solution

    Set a cookie:

    function createCookie(name,value,days)
          if (days)
                var date = new Date();
                var expires = "; expires="+date.toGMTString();
          else var expires = "";
          document.cookie = name+"="+value+expires+"; path=/";

    function readCookie(name)
          var nameEQ = name + "=";
          var ca = document.cookie.split(';');
          for(var i=0;i < ca.length;i++)
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
          return null;

    if (readCookie("SeenPopup") == null)
      // Place your code here.
      createCookie("SeenPopup", 1, 1);  // Set a cookie to indicate window seen. This should be inside your if statement.

    The JavaScript functions were borrowed from this article:

    Hope this helps,

    LVL 44

    Expert Comment

    However, while the 'cookie' approach might seem attractive, there are MANY users of IE who, for one reason or another, do NOT allow cookies on their machine, and the cookie approach would then NOT work.

    A 'cookie-less' approach might be to pass a parameter in a) the Session value or b) the QueryString for each page, indicating that this user had (or had not) been asked to take the survey.

    LVL 5

    Expert Comment

    Are you sure? Who are these cookie-less users? Have you ever actually met one? If someone is paranoid enough they don't trust cookies, they've probably also turned off JavaScript, so your pop-ups won't work anyway.

    An awful lot of websites rely on cookies for their session management, so if you turn off cookies, there's a fair portion of the web that won't work for you. I think the myth that cookies allow websites to read your personal files has been well and truly busted at this stage. While there is some concern over cookies being used to track your browsing habits, most browsers now offer some level of protection from this.

    If you want to go without cookies, you probably need a server-side approach. You could use session variables, either in PHP, ASP or something else, but in most cases these also require cookies (ASP.NET offers a cookie-free session mode based on URLs, but it's since been discovered that this can by be used for a form of phishing, and it's use is not recommended).

    If you find that your users can't use cookies, then you need to look at other approaches. However, if they work for the vast majority of users, they're a simple way to achieve what you want without significant reengineering your application.

    Author Comment

    Hi James

    Thanks a lot! It worked great.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article will show, step by step, how to integrate R code into a R Sweave document
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now