Please help me understand why my code does not redirect to a specific link.

On the page http://myhost.ru/brands/ there is something like this script (I quote a part)

console.log(hostnameStr); // 'http://myhost.ru' console.log(pathStr); // '/brands' console.log(brandStr); // '/BlackBerry' alert(hostnameStr + pathStr + brandStr); document.location.href = hostnameStr + pathStr + brandStr; 

Before this code, I define hostnameStr , pathStr , brandStr . Then, as you can see, I bring them to the console and to the modal window. At the same time the address is shown correct

But the last line redirects to about that address.

 http://myhost.ru/myhost.ru/brands/BlackBerry 

According to my idea, the redirection should occur at such an address.

 http://myhost.ru/brands/BlackBerry 

    2 answers 2

    document.location , window.location

    therefore:

     document.location=''+hostnameStr + pathStr + brandStr; 

    http://jsfiddle.net/oceog/yDELf/

    and considering

    document, localization was not a property, although it is a gecko browser. For cross-browser safety, use window.location instead.

    will be better

      window.location=''+hostnameStr + pathStr + brandStr; 

      Redirect without host:

       document.location.href = pathStr + brandStr; 
      • one
        it is yes but I would like to understand the reason. The question in this was - cyklop77