• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Javascript popup only once for one user/session

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.

1 Solution
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.
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,

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.

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.
momer123Author Commented:
Hi James

Thanks a lot! It worked great.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now