简体   繁体   中英

window.location.replace not working on localhost

I want to navigate to a URL on my site. This is what I have:

var TheDemoURL = window.location.host;

if (SomeCondition1) { TheDemoURL = TheDemoURL + '/fr/demo'; }
if (SomeCondition2) { TheDemoURL = TheDemoURL + '/de/demo'; }
...

window.location.replace(TheDemoURL);

Initially, in the variable watch, I have TheDemoURL: "localhost:49173" and when I alert the final TheDemoURL is looks a good URL but in reality nothing happens.

Why is this not working?

Ok, for those who come here, the solution was to add this:

var TheDemoURL = window.location.protocol + '//' + window.location.host;

Not sure if this is specific to asp.net but it made it work.

Try using

self.location = TheDemoURL;

This will take into account iframes and other weirdness.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM