Wednesday, February 13, 2008

Show confirmation dialog box to the user using javascript

Many a times we need to ask a yes or no dialog box to the user. We can achieve this for instance using javascript.

Below is a simple script which is called in the onClick event of a button.

If user presses OK, the page will be submitted and continue process. If user presses NO, no action will take place and user remains on the same page.

<script type="text/javascript">
function confirmLeavePage()
{
return confirm("Any changes you have made may be lost. Please click the Cancel button to return to the page to save your changes or select OK to proceed without saving your changes");

}
</script>

<button onclick="if(!confirmLeavePage())return false;" name="aButton" type="submit">Next Page</button>

No comments: