Ryan Shine
Ryan Shine

Reputation: 522

call the javascript alert and redirect url issue

Hi ladies and gentlemen,

I am trying to call the alert from the button, and after the alert I want it to redirect to my home page. However when I call the below code from asp button

ClientScript.RegisterStartupScript(this.GetType(), "Success", "alert('" + "Activation mail has been sent to the email address. Please check your email!" + "');window.location.href('Home.aspx');", true);

It doesn't work. The alert can be call so this javascript is correct. What I suspect is the postback cause the redirect doesn't work. So I put the update panel as the result the button cannot be call the function at all.

In the end I tried to put return false in the javascript also not working.

I also tried to put response redirect after call the javascript function but the javascript and the behind code running synchronize so the page will be redirect before the alert will call.

How I can achieve my scenario?

Upvotes: 1

Views: 1552

Answers (4)

Charly
Charly

Reputation: 7

I suggest you instead of using

window.location.href('')

Use

window.location.replace('')

if you want to redirect like an html redirect.
Source: How to redirect to another webpage in JavaScript/jQuery?

However, like says you can use :

window.location ='Home.aspx'

Upvotes: 0

user1023602
user1023602

Reputation:

As the other answers say:

 window.location.href = 'Home.aspx'

However that is a relative path, so it only works if the current page is in the same folder as the Home page. Redirecting to a relative URL in JavaScript.

Use '/' to go to the root folder of your website:

 window.location.href = '/Home.aspx'

Upvotes: 0

Racil Hilan
Racil Hilan

Reputation: 25351

href is not a function, so you cannot call it like href('Home.aspx'). Change

window.location.href('Home.aspx')

to

window.location ='Home.aspx'

Upvotes: 0

Jigneshk
Jigneshk

Reputation: 348

try this.

ClientScript.RegisterStartupScript(this.GetType(), "Success", "alert('Activation mail has been sent to the email address. Please check your email!');window.location ='Home.aspx';", true);

Upvotes: 1

Related Questions