Suppose you have a link in your application or pae saying Edit page. You want user to confirm say yes before he go to that link after clicking so there is simple JavaScript.
Note javaScript have default confirm function confirm(); which returns true or false if user clicks ok this returns true otherwise this will return false, let’s create a function saying confirmIt(); with javaScript you can define this function before </head> section in html or inside your content of body. See function below.
[code lang=”js”]<script type="text/css">
function confirmIt() {
confirm(‘Are you sure you want to process this? You will not able to undo this process.’);
}
</script>[/code]
Ok this function of javaScript will prompt an alert box to get confirmation from user if he want to proceed or not. Now we need to call this function in both our href link and in our form submittion.
Calling javaScript function from html link.
You can call function via link with onclick=”” attribute. so calling above function in edit page link will work like this.
[code]<a href="edit_page.php" onclick="return confirmIt();">Edit Page</a>[/code]
As you know confirm() returns true or false so this link process will depends on user answer.
Calling JavaScript function from html form.
To call JavaScript function via html form you have to call function and return value on onsubmit=”” form attribute like below.
[code]<form action="process_user.php" method="post" onsubmit="return confirmIt();"></form>[/code]
When user will click submit button for this form above function would be called and will return a value depends on user’s answer.