Javascript location.href not working

  • Thread starter Thread starter Darin Browne
  • Start date Start date
D

Darin Browne

Here is my code created when a page loads the first time:

string jScript =
@"

<script
language='javaScript'>
var pop = false;


function
CancelPage()
{
var ans =
false;
ans =
confirm('Are you sure you want to Cancel and lose any
changes?');
if (ans)
{

alert('yes');

parent.location.href = 'OrderMisc.aspx';
}
}
</script>";

if ( !IsStartupScriptRegistered
("CancelPage") )
RegisterStartupScript
("CancelPage", jScript);

btnCancel.Attributes.Add
("onclick","javascript: pop=true;CancelPage();");
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Code is called correctly when the Cancel button is
pressed because I get the confirmation box, I click yes
and I get the alert box from the code, then the page
reloads without redirecting to 'OrderMaint.aspx'.

OrderMaint.aspx is spelled correctly and in the same
directory so I'm not sure why it's not taking me there.

Any ideas?

Thanks,
Darin
 
Hi,

If button is asp.net server control or HTML submit button you need to
cancel it default behavior by cancel event bubbling and returning false
from your javascript function:

window.event.returnValue = false;
window.event.cancelBubble = true;


Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
Back
Top