Monday, March 29, 2010

Customize Javascript Confirm Box

Main.html
<html>
<head>
<title>
Customized Javascript Confirm Box Example
</title>
<script type="text/javascript">
function doDelete()
{
var returnVal=0;
if(window.showModalDialog)
{
returnVal=window.showModalDialog("YesOrNo.html","name","dialogWidth:455px;dialogHeight:140px");
}
if(returnVal==1)
{
alert("Deletion Succesfully Done");
// write your own functionality here
}
else
{
        alert("Deletion Cancelled");
// write your own functionality here
}
}
</script>
<body>
<font size="5">
<pre>
By clicking the bellow button you can observe customized javascript confirm dialog box
for deleting or some functionality what you require in your application.
                              

                                 <input type="button" value="Delete" onclick="doDelete();" style="font:20px"/>
</font>
</pre>

YesOrNo.html
<html>
<script>
function yesDelete(){
window.returnValue = 1; 
window.close();
return false;
}
function noDelete() {
window.returnValue = 0;  
window.close();
return false;
}
</script>
<body>
<div style="position:relative;left:0px;top:0px;background:#000066;color:#FFFFFF;font-size:20px;font-weight:bold;width:396px;height:25px;padding-left:38px;border-bottom:solid 2px #000000;">Confirm Delete...</div>
<blockquote style="font-size:18px;font-weight:bold;color:#FF0000;margin-top:50px;">Are you sure you wish to Delete this Record?</blockquote>
<div style="width:396px;text-align:center;">
<input type="button" value="    YES    " onclick="yesDelete();"  style="font-weight:bold;margin-right:10px;cursor:pointer;"><input type="button" value="     NO     " style="font-weight:bold;margin-left:10px;cursor:pointer;" onclick="noDelete();">
</div>
</body>
</html>

Both Main.html and YesOrNo.html files should be in the same folder.

2 comments:

  1. I used this in onBeforeUnload event, when user click any link/button to navigate away from this page, it ask this confirmation. But in both Yes and No buttons, it navigates to another page. How can I stop this in No button? What I want is when user press NO, he should remain in the same page.

    ReplyDelete
  2. see following links

    http://msdn.microsoft.com/en-us/library
    /ms536907(VS.85).aspx

    http://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own

    if you want any detail clarification i need sample code what you used in html pages,thank you

    ReplyDelete