www.webdeveloper.com
Results 1 to 9 of 9

Thread: Javascript instead of Ok/Cancel Confirmation box how to use Yes/No

  1. #1
    Join Date
    Jul 2005
    Posts
    4

    Javascript instead of Ok/Cancel Confirmation box how to use Yes/No

    Hi friends

    In the Javascript instead of Ok/Cancel Confirmation box how to use Yes/No Confirmation box.

  2. #2
    Code:
    <HTML>
    <Head>
    <Script Language=JavaScript>
    
    	var isChoice = 0;
    	
    	function callAlert(Msg,Title){
    		
    		txt = Msg;
    		caption = Title;
    		vbMsg(txt,caption)
    		alert(isChoice);
    		
    	}
    </Script>
    
    <Script Language=VBScript>
    
    	Function vbMsg(isTxt,isCaption)
    	
    	testVal = MsgBox(isTxt,3,isCaption)
    	isChoice = testVal
    
    	End Function
    
    </Script>
    </Head>
    <Body>
    <input type=button value="Test" onclick="callAlert('Can you get there from here?','This is a Title')">
    </Body>
    </HTML>

  3. #3
    Join Date
    May 2006
    Posts
    1

    Problem with FireFox

    Hi,

    The given code is perfectly working in IE But in firefox it is not working , giving error message vbMsg is not defined.

    Is there any other solution.......

  4. #4
    Join Date
    Feb 2006
    Posts
    2,927
    This might help:
    http://www.yankeeweb.com/ghost_2.html

    Samples and code for faux dialogs
    Last edited by mrhoo; 06-27-2006 at 01:05 AM.

  5. #5
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    The first solution given used VB which only runs in IE. For a cross browser solution you have to create the popup yourself because Javascript doesn't have a Yes/No dialog. A simpler way is to just reword the question so that OK and cancel make sense. So instead of asking "Do you want to ..." you ask "Are you sure you want to ...".

  6. #6
    Join Date
    Jun 2006
    Posts
    106
    HI guys..i also would like to have yes/no button which can run on IE and several other browser....

    in other word, i would have to run certain code whenever the user click on yes or no button...(i need to update the dbase)..

    unlike in normal yes /cancel dialog box....in that dialog box, when the user click on the cancel button ..no code will be executed....

    anyone got clues on how to do this apart from using vb script???cause vb script only runs on IE....



    thanks for all....
    Last edited by JPnyc; 06-26-2006 at 10:06 PM. Reason: it's not a good idea to post contact info on the web.

  7. #7
    Join Date
    Dec 2009
    Posts
    4

    How to display Javascript Yes/No confirm dialog instead of Ok/Cancel ?

    Write the following code in your HTML Page.

    <script language="javascript" type="text/javascript">
    function window.confirm(str)
    {
    execScript('n = msgbox("'+str+'","4132")', "vbscript");
    return(n == 6);
    }
    </script>

    Call the function from the onClientClick event of your button

    <asp:Button ID="btnOk" runat="server" OnClientClick="javascript:return confirm('Are you sure to proceed?');" OnClick="btnOk_Click" Text="OK" />

    Write the btnOk_Click event in your server side code

    protected void btnOk_Click(object sender, EventArgs e)
    {

    // your code
    }
    Now your confirmation dialog will show Yes/No instead of Ok/Cancel. If you click Yes, the control will go to the server side method and if you click No, the server side code will not execute.

    Eliza

  8. #8
    Join Date
    Oct 2006
    Posts
    3
    so the only way to do this is with VBscript?
    to cross browser solution for javascript?

  9. #9
    Join Date
    Feb 2006
    Posts
    2,927
    I include this because the link to my answer from 7 years ago is broken.

    It really is not worth the bother.

    Code:
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset= "utf-8">
    <title>Small Page</title>
    <style>
    #modblocker{position:absolute;top:0;left:0;width:100%;z-index:1000000;
    background-color:rgb(255,255,255);opacity:.25; filter:alpha(opacity=25)}
    #modwin{position:absolute;width:300px;height:200px;z-index:1000000000;
    background-color:#cfcfcf;border:5px navy ridge;
    }
    button{font-weight:bold;color:black;cursor:pointer;}
    button:hover, button:focus{color:red;}
    </style>
    </head>
    <body>
    <h1>Small Page</h1>
    <p><button type="button">Yes or No?</button><br>
    <h2 id="votespan">Count:</h2>
    <ul class="breadcrumbs">
        <li><a href="../webshop.html" title="the Yankee Webshop">Home</a></li>
        <li><a href="../index.html">Areoka</a></li>
        <li><a href="../defguide.html?nocode">HTML Elements</a></li>
    </ul>
    <div>
        <p><strong>"Here's another fine mess you've gotten us in."- <em>
        Oliver Hardy</em></strong>
        </p>
    </div>
    <script>
    
    function yesOrNo(msg, cb){
        var d= document.documentElement, b= document.body;
        var pa=document.createElement('div');
        pa.id='modblocker';
        pa.style.cssText='width:'+d.offsetWidth+'px;height:'+d.offsetHeight+'px;';
        
        if(b.clientHeight>d.clientHeight) d= b;
        var w= Math.floor((d.clientWidth-300)/2), s= d.scrollTop,
        h= Math.floor((d.clientHeight-250)/2)+s;
        var modwin= document.createElement('div');
        modwin.id= 'modwin';
        modwin.style.cssText= 'top:'+h+'px;left:'+w+'px';
        var inh= '<p style="text-align:right"><button type="button">No</button>'+
        '<button type="button" class="trueClass">Yes</button>'+
        '<button type="button" class="cancelClass">Neither</button></p>'+
        '<h3 style="text-align:center">'+msg+'</h3>';
        modwin.innerHTML= inh;
        document.body.appendChild(pa);    
        document.body.appendChild(modwin);
        modwin.onclick= function(e){
            e= e || window.event;
            var value, pa, who= e.target || e.srcElement;
            if(who.tagName== 'BUTTON'){
                if(e.stopPropagation) e.stopPropagation();
                else e.cancelBubble= true;
                if(who.className== 'cancelClass') value= 'Quit';
                else value= who.className== 'trueClass';
                document.body.removeChild(document.getElementById('modwin'));            
                document.body.removeChild(document.getElementById('modblocker'));
                
                if(typeof cb== 'function') return cb(value);
            }
        }
        modwin.getElementsByTagName('button')[0].focus();
    }
    function sample1(arg){
        if(arg==='Quit') return;
        sample1.upvotes= sample1.upvotes || 0;
        sample1.downvotes= sample1.downvotes || 0;
        if(typeof arg== 'boolean'){
            if(arg) sample1.upvotes+= 1;
            else sample1.downvotes+= 1;
            var v='Yes= '+sample1.upvotes+', No= '+sample1.downvotes;
            document.getElementById('votespan').childNodes[0].data=v;
        }
        else{
            sample1.arg= typeof arg=='string'? arg : 'Do you think its worth it?';
            yesOrNo(sample1.arg+'\n', sample1);
        }
    }
    window.onload=function(){
        document.getElementsByTagName('button')[0].onclick=sample1;
    }
    </script>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles