Dear All,

I have a problem with delete confirmation code in java script.

i m using this code:

<script LANGUAGE="JavaScript">

function confirmSubmit()
{
var agree=confirm("Are you sure you wish to continue?");
if (agree)
	return true ;
else
	return false ;
	
}

</script>

in Asp page on click of hyperlink tage message displays and on click of Cancel the next page is called where as in simple html file hyper tag code acts properly.

please suggest in this regars what i have to day and where i m making mistake

Dani AI

Generated

Short diagnosis and what to check

The symptom you describe (Cancel still navigates) almost always means the browser never sees the click handler's boolean result — so the link's default action or a server-side postback still runs. was on the right track: the handler must deliver a boolean back to the event so the browser can cancel the navigation. ’s idea to call the form submit is fine, but be careful: calling submit() inside a handler will always navigate if it runs, so you must only call it when the user confirmed.

A robust, modern approach

If you can change the client HTML/JS, attach a click listener and call preventDefault() when the user cancels. This avoids inline-return pitfalls and works whether the element is a plain link or an element styled as a link:

document.querySelectorAll('.need-confirm').forEach(function(el){
  el.addEventListener('click', function(e){
    if (!window.confirm('Delete this item?')) {
      e.preventDefault();    // stop navigation/postback
    }
  });
});

ASP.NET specifics

If the clickable is an ASP.NET server control that renders a postback (look for __doPostBack in the page source), wire a client-side confirmation that prevents the postback. Use the control’s client hook (OnClientClick or equivalent) so the confirmation runs before ASP.NET’s generated script. If the client script returns false, the postback is cancelled; otherwise the server receives the request and your Cancel will be ignored.

Quick troubleshooting checklist

  • View rendered HTML (right-click → View Source) and search for __doPostBack.
  • In DevTools, inspect Event Listeners on the element to see other handlers.
  • Temporarily log to console from your handler to confirm it executes.
  • If problems persist, show the rendered element markup so the exact fix (inline return vs. OnClientClick vs. preventDefault) can be recommended.

References: MDN confirm and preventDefault docs and Microsoft docs for client hooks are useful reading if you want the formal behavior details: https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm (confirm), https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault (preventDefault), https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.button.onclientclick?view=netframework-4.8 (ASP.NET client hook).

Recommended Answers

All 2 Replies

What about just doing something like: <a href="delete.asp" onclick="return confirm('Are you sure you wish to continue?')">Delete</a> Or, if you need a function for several links:

<script type="text/javascript">
function confirmSubmit(){
return confirm('Are you sure you wish to continue?');
}
</script>
<a href="delete.asp" onclick="return confirmSubmit();">Delete</a>

Try like this:

<script language="JavaScript" type="text/javascript">
var d = document.form1;						
	function del(x)
	{
						if(confirm("You want to delete this record"))
					{
					d.submit();
					}
				
			return;
	}
        </script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.