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,
Web BrowsersJavaScriptWeb Languages and Standards

Avatar of undefined
Last Comment
Meritor

8/22/2022 - Mon
brad2575

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');
}
brad2575

Sorry the syntax may have been wrong.

try

if(window.document.form.checkboxname.checked){
ASKER
patilarvindg

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Meritor

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

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
ASKER
patilarvindg

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Meritor

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question