SetiSeeker
SetiSeeker

Reputation: 6684

How do I do a Javascript Redirect in asp.net

I have a asp.net webform with a button. The OnClientClick event is wired to a javascript function.

I need this function to redirect the current page to this page.

After much reading is have tried all the following, but to no avail:

           var url = "http://www.google.com";
           document.location = url; //Doesn't Work
           document.location.href = url; //Doesn't Work
           window.location = url; //Doesnt Work
           window.location.href = url; //Doesnt Work

Any Help or advice are welcome.

Ps. I am entering the JS function as my alert message pops up

Upvotes: 2

Views: 8126

Answers (4)

Vladimir Kocjancic
Vladimir Kocjancic

Reputation: 1842

You can try location.replace(myUrl);, but document.location.href should really work.

Upvotes: 0

Stu
Stu

Reputation: 69

Try putting return false; after your function in the OnClientClick

e.g. OnClientClick="yourfunction();return false;"

Upvotes: 1

Aristos
Aristos

Reputation: 66649

You probably have somewhere on your code a Javascript Error, that stop the execution, because as the other say, this code that you say that "doesnt work", it's work fine.

Upvotes: 1

Krishna Kumar
Krishna Kumar

Reputation: 8211

I tried this in a html page and it worked fine.

function go() { alert(); var url = "http://www.google.com"; document.location.href = url; //Doesn't Work }

Just look at the page source and see whether onClientClick is calling your js function

Upvotes: 1

Related Questions