Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Alert limitations

    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.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Code:
    <!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">
    <head>
    <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();
    		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;
    }
    function isCookie(name)
    {
    	var theCookie = readCookie(name);
    	if(theCookie != 'true')
    	{
    		alert('You have pressed this button once, you will not see this alert again');
    		createCookie(name,'true',9999)
    	}
    }
    function eraseCookie(name)
    {
    	createCookie(name,"",-1);
    }
    </script>
    </head>
    
    <body>
    <input type="button" onclick="isCookie('done')" value="Click Me" />
    </body>
    </html>
    You can use cookies.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    you can also remove the onclick handler when they click it:

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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks guys


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •