[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 868
  • Last Modified:

javascript alert does not always display

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

0
levelninesports
Asked:
levelninesports
1 Solution
 
bombozamaCommented:
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

0
 
rhodesbCommented:
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.
0
 
quincydudeCommented:
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.


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
levelninesportsAuthor Commented:
would the onsubmit event catch any more users that the onclick event is missing?
0
 
quincydudeCommented:
Formally using onsubmit in this case is better.
0
 
rhodesbCommented:
Shouldn't make any difference.  Both are javascript triggers and if javascript is not on in the browser, then neither will work.
0
 
rhodesbCommented:
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.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now