1

I have an ASP.NET MVC 5 application in which I'd like to perform a redirection using Javascript:

var sPageURL = decodeURIComponent(window.location);
console.log(sPageURL);
var lengthUrl = sPageURL.split('/').length;
var NewUrl = '';
sPageURL.split('/').forEach(function(item, index) {
  if (index < lengthUrl - 2) NewUrl += item;
  if (index == lengthUrl - 1) {
    if (item == "Organisateur") NewUrl += sPageURL.split('/')[lengthUrl - 2];
  }
});
console.log(NewUrl);
window.location = NewUrl;

I get as output:

http://localhost:31569/Event/2

http://localhost:31569/Event/localhost:31569

The problem is that the new URL is concatenated with the old one: I'd like http://localhost:31569/Event/2 be replaced by localhost:31569.

I tried window.location = , window.location.href = and window.location.replace and I get the same result .

So I need to know:

  1. What is the reason of this problem ?
  2. How can I fix it?
2
  • 1
    Why don't you just do window.location = "/" if you want to navigate to the root of the URL? Commented Jun 11, 2016 at 14:27
  • @KianCross good it works : a simple and good solution. But I still not understand what is the problem with my code ( plz post your comment as an answer) Commented Jun 11, 2016 at 14:33

1 Answer 1

1

Using window.location = "/" should navigate to the host name of the web page, which I think is what you're trying to do.

The problem you are having sounds like you have not included the protocol (http:// or https://) in front of the URL you want to navigate to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.