Link to home
Start Free TrialLog in
Avatar of momer123
momer123

asked on

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) {
      <<<POPUP WITH WINDOW>>>
};


}

</script>


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

Avatar of david_levine
david_levine

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.
ASKER CERTIFIED SOLUTION
Avatar of lostcarpark
lostcarpark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Avatar of momer123

ASKER

Hi James

Thanks a lot! It worked great.