samedi 11 juin 2016

Redirection using javascript

I have a 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 that 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?

Aucun commentaire:

Enregistrer un commentaire