View Full Version : Alert limitations

05-03-2006, 06:48 PM
Can I limit the number of times an "alert" is activated.

I have a button which a user is likely to click more than once. The user needs to be aware of some facts, so I have an "alert" set up.

But I don't want them to have to view the "alert" every time they click the button.

Is it possible to have the "alert" appear only once.

05-03-2006, 07:18 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
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;
function isCookie(name)
var theCookie = readCookie(name);
if(theCookie != 'true')
alert('You have pressed this button once, you will not see this alert again');
function eraseCookie(name)

<input type="button" onclick="isCookie('done')" value="Click Me" />

You can use cookies.

05-03-2006, 08:06 PM
you can also remove the onclick handler when they click it:

<input type="button" onclick="alertfunction();this.onclick=null;" />

Philip M
05-03-2006, 09:15 PM
With respect, neither of these solutions seems to be the answer to the question asked.

"I have a button which a user is likely to click more than once. Is it possible to have the "alert" appear only once." (But I assume whatever else the button does is still wanted).

Surely the simplest is to add in to the function which is called by the button:-

var clicked = 0
function whatever() {
if clicked == 0 {
alert ("The alert message");
clicked = 1;
..... and then whatever else the button does

05-03-2006, 11:25 PM
Thanks guys