DelphiFAQ Home Search:

Have a Javascript popup display Yes/ No instead of Confirm/ Cancel

 

comments26 comments. Current rating: 5 stars (6 votes). Leave comments and/ or rate it.

Question:

I need to ask my web site's user a Yes/ No question. Currently I use JavaScript's confirm() function.
The return value is true (OK) or false (CANCEL).
The word CANCEL is misleading. I want to have the buttons say Yes/ No instead.
How can I do it?

Answer:

Many people will tell you it is not possible, and that is true for standard Javascript.

However, I found the following which works at least in 32 bit Windows with IE (which often accounts for 95% of your site's users).

This solution uses vbscript's confirm() function.

No, it does not work in FireFox. FireFox will display Confirm/ Cancel.

<script language=javascript>

/*@cc_on @*/
/*@if (@_win32 && @_jscript_version>=5)

function window.confirm(str)
{
    execScript('n = msgbox("'+str+'","4132")', "vbscript");
    return(n == 6);
}

@end @*/
var r = confirm("Can you do it?");
alert(r);
</script>

Comments:

You are on page 1 of 2, other pages: [1] 2
2006-12-15, 17:58:08
anonymous from United States  
Very nice!!!! Thank you.
2006-12-22, 07:40:16
anonymous from Belgium  
You're brilliant!
2007-03-28, 05:27:41
anonymous from  
So nice! :)
2007-04-11, 00:18:27
anonymous from Thailand  
Thank U .It works well
but how can I get a new line
When I use '\n' in my message(str)
It show an error 'Unterminated string constant'
2007-04-19, 12:40:25
Raj from United States  
Thanks
2007-07-27, 03:42:18
anonymous from Calcutta, India  
Excellent piece of code!!!!

Cheers :-)
Shaunak
2007-07-31, 06:05:39
seth from United States  
can someone please EXPLAIN this code? i have no idea what you're doing with this 'vbscript' thing.
2007-11-02, 11:00:36
[hidden] from United States  
rating
Super Sweet code! Is there a way to get it to work on an onClick vs. window load?
2007-11-07, 07:49:09   (updated: 2007-11-07, 07:50:25)
anonymous from United Kingdom  
Great help thanks.


> Is there a way to get it to work on an onClick vs. window load?
Remove these lines:
var r = confirm('Can you do it?');
alert(r);


Use as:
OnClick='confirm('Can you do it?');'
Note that speach marks don't display in this posting.
2007-11-13, 00:23:13   (updated: 2007-11-13, 00:25:14)
anonymous from Singapore  
rating
Thanks.Many people say,can not do.u did
Brilliant.Great job!
2007-11-13, 00:31:53
anonymous from Kuala Lumpur, Malaysia  
rating
This code very nice.
Thanks a lot =)

Add this in <Head> tag:

<script language=javascript>
/*@cc_on @*/
/*@if (@_win32 && @_jscript_version>=5)

function window.confirm(str)
{
execScript('n = msgbox(''+str+'','4132')', 'vbscript');
return(n == 6);
}
@end @*/
</script>

Any confirmation in same page, will appear with Yes/No button... Great !!

Keywords:

2007-11-13, 15:04:34
anonymous from United States  
I really need help on this. What's the full code ? I am getting error-
I have following inside the Head tag
<script language=javascript>
/*@cc_on @*/
/*@if (@_win32 && @_jscript_version>=5)

function window.confirm(str)
{
execScript('n = msgbox(''+str+'','4132')', 'vbscript');
return(n == 6);
}
@end @*/
function confirmSubmit()
{
var agree=confirm('Are you sure you wish to continue?');
if (agree)
    return true ;
else
    return false ;
}

</script>



And I am calling
<input type='Submit' name='Delete' value='Delete' onClick='return confirmSubmit()'>

and getting error. Can you please show the correct way.
2007-11-13, 15:35:31
anonymous from United States  
Never Mind I got it working but Yes option is working properly but no button is not behaving properly. It's acting like yes button. Any idea?
2007-12-03, 17:57:15
[hidden] from Indonesia  
        <script language='javascript'>
            function confirmExt()
            {
                if (Form1.document.getElementById('txtTemp').value == 'run')
                {
                    if(window.confirm('Are you sure want to change this file extention? Please be aware that changing the extension will affect the file readability.'))
                    {
                        document.getElementById('txtTemp').value = 'change';
                        Form1.submit();
                    }
                    else
                    {
                        document.getElementById('txtTemp').value = 'cancel';
                        window.history.back;
                    }
                }
            }
        </script>

anyone can help me ?
i use this javascript. it work in IE but in FF is not work. anything wrong or what i must to set to work in FF ?

thanks
2007-12-31, 04:48:55
jprk_prabhu@yahoo.co.in from India  
rating
NIce.
You are on page 1 of 2, other pages: [1] 2

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.