?
Solved

Javascript popup only once for one user/session

Posted on 2006-05-26
5
Medium Priority
?
345 Views
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) {
      <<<POPUP WITH WINDOW>>>
};


}

</script>


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

0
Comment
Question by:momer123
5 Comments
 
LVL 9

Expert Comment

by:david_levine
ID: 16770497
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.
0
 
LVL 5

Accepted Solution

by:
lostcarpark earned 500 total points
ID: 16770584
Set a cookie:

function createCookie(name,value,days)
{
      if (days)
      {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            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:

http://www.quirksmode.org/js/cookies.html


Hope this helps,

James
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16770674
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.

AW
0
 
LVL 5

Expert Comment

by:lostcarpark
ID: 16772174
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.
0
 

Author Comment

by:momer123
ID: 16772900
Hi James

Thanks a lot! It worked great.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Introduction to Processes
Starting up a Project

830 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