We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

javascript alert does not always display

levelninesports
on
Medium Priority
889 Views
Last Modified: 2012-05-06
I have a javascript alert that is not always popping up for users. I do not know the conditions under which the alert is not working, but for some users it is not. Just wondering if there is a more foolproof way to display a message to a user if a click occurs without going to a different page.
Is there a not javascript method that will always display? It's possible that the only users who are not getting the message are those with javascript disabled or who had previously seen the message.
<input type="radio" name="payment" value="moneyorder" onClick="javascript:alert('You have selected NOT to pay with a Credit Card, this will delay your order!')">

Open in new window

Comment
Watch Question

I've seen that you get different behaviors from different browsers. Specially with radio buttons and checkboxes with the onClick event handler.

I deal with it in a very "strange" way, maybe it could help you. What I do is replace the radio or checkbox with an image and give it an onclick function that does two things:

1.- replace the image with the selected state (as in checked / unchecked, pressed / unpressed)
2.- swap a hidden input's value from one to zero according to the case.

It looks something like this:
#Javascript
 
function swapValues(){
	image = document.getElementById('img1');
	hidden = document.getElementById('hiddenField1');
	if(hidden.value==0){ //unpressed state
			hidden.value = 1;
			image.src = 'pressed.gif';
			alert('You have selected NOT to pay with a Credit Card, this will delay your order!');
		}
	else{
			hidden.value = 0;
			image.src = 'unpressed.gif';
		}
	}

Open in new window

Commented:
There appears to be nothing wrong with your alert code.  It should pop up every time it is clicked regardless of whether or not they've seen it before.  Obviously having javascript disabled will not allow the alert to work.  I've also had problems before where there was actually a javascript error elsewhere on the page that causes unrelated javascript to not work properly.  Specifically the problems are more likely if there are closing braces '}' missing at the end of a function or something.
Your code should be fine. It's just the client side issue.

Another approach is to check on form submission. Add the onsubmit event and prompt a confirm box showing the same message, so form can only proceed if user clicked OK on the box.


Author

Commented:
would the onsubmit event catch any more users that the onclick event is missing?
Formally using onsubmit in this case is better.

Commented:
Shouldn't make any difference.  Both are javascript triggers and if javascript is not on in the browser, then neither will work.
Commented:
You could do some trickery with server-side code where when they click it does a submit and then reposts the form with the error message in a div or p or span or whatever.  If you are coding is asp.net, this is really easy.  If not, it can still be done, it just requires a little more code.  With server-side code, you won't get the pop-up but it will work every time.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.