Link to home
Start Free TrialLog in
Avatar of patilarvindg
patilarvindg

asked on

How to have an option to disable ALERT box in javascript (Do not show next time)

I want to show an alert box with some message and a check box "Never show this hint again" which should be unchecked by default but on selecting that check box, ALERT should not appear next time.
 Please give me code which can handle this event.
Thanks,
Avatar of brad2575
brad2575
Flag of United States of America image

Are you saving this setting somewhere?  I am going to assume you only want the JavaScript that checks this checkbox value (and you are determining if it is selected or not each time the person comes back (remembers if they checked it or not) next time.

The code to check would be:

if(window.document.form.checkboxname.checked == true){
    // do NOT show alert
}else{
   alert('show alert here');
}
Sorry the syntax may have been wrong.

try

if(window.document.form.checkboxname.checked){
Avatar of patilarvindg
patilarvindg

ASKER

Could you please give me some more detail on saving the state of the checkbox?
I want a alert box with checkbox, if checked nd click OK, alert should not appear next time on the same machime.
NO, JavaScript does not provide alert with checkbox. You need to do it programatically.
Try following code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Alert Example </TITLE>
 </HEAD>
 
 <BODY>
 <a href="javascript:showAlert()">Show Alert</a><br/>
 <a href="javascript:window.location.reload();">Refresh Page</a>
  <SCRIPT LANGUAGE="JavaScript">
  <!--
//Global Variable
var isChecked = false;
 
function showAlert()
{
	if (!isChecked)
	{
		var alertBox = window.open("","","toolbar=no,menubar=no,width=300,height=110,modal=yes'");
		alertBox.document.open();
		alertBox.document.write('<HTML>');
		alertBox.document.write('<BODY>');
		alertBox.document.write('<center><h5>Your Alert Message</h5>');
		alertBox.document.write('<input type="button" name="OK" value="OK" onClick="window.close();"><br/>');
		alertBox.document.write('<input type="checkbox" name="hideAlertChkBox" onClick="setChecked(this.value)">');
		alertBox.document.write('Do not show this alert again!');
		alertBox.document.write('</input></center>');
		alertBox.document.write('<script>function setChecked(isChecked) {');
		alertBox.document.write('window.opener.isChecked=isChecked;');
		alertBox.document.write('}</script>');
		alertBox.document.write('</BODY>');
		alertBox.document.write('</HTML>');
		alertBox.document.close();
	}
}
 
 
  //-->
  </SCRIPT>
 </BODY>
</HTML>

Open in new window

Avatar of b0lsc0tt
patilarvindg,

When does this alert appear?  If when the page loads or if you want to have the browser remember this even when the visitor comes back to the page then you will have a problem.  Javascript won't remember what happened when you were at the page before.

A solution is to use a cookie to store the fact the box was checked.  Even this won't last forever and can be deleted so there isn't really a way to "never" show it again.

Let me know if you have any questions or need more information.  I can provide some info on using cookies in Javascript but a good place to start is http://www.quirksmode.org/js/cookies.html .

b0lsc0tt
Hi Meritor,
I want similar functionality as you listed in comment above BUT I want checkbox state to be saved in the form of a cookie. So that as long as cookie is valid, alert should not appear.  Could you please give me modified code for it.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Meritor
Meritor
Flag of India image

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