Lamloumi Afif Lamloumi Afif - 1 year ago 57
Javascript Question

Redirection using javascript

I have a mvc 5 application in which I'd like to perform a redirection using javascript

var sPageURL = decodeURIComponent(window.location);
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];
window.location = NewUrl;

I get as output :



The problem is that the new url is concatenated with the old one, I'd like that
be replaced by

I tried
window.location =
window.location.href =
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?

Answer Source

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 http:// or https:// is not included in front of the URL you want to navigate to, therefore it is navigating to it relative to the current URL.