我想将用户从不同的 url 重定向到特定的 url。我尝试了各种替换方式,但似乎无法获得我想要的行为。除非我提供主机名,否则此代码有效。我想使用 windows.location.hostname 中的现有主机名,并只提供一个新的路径名。有时,网址的大小和斜线(“/”)会有所不同。
window.location = 'http://localhost:36065/NewPath';
我将如何更改这些网址?
http://somesite.com/xxx/yyy/zzz to http://somesite.com/NewPath
http://somesite.com/xxx/yyy to http://somesite.com/NewPath
http://somesite.com/xxx to http://somesite.com/NewPath
我想你明白了。路径可能因路径而异,我想基本上用“NewPath”替换 .com 之后的所有内容
如果可能的话,我想要一个干净的正则表达式解决方案,但我是那个部门的菜鸟。感谢您提供任何提示或技巧。
最佳答案
location.pathname = '/newpath.html'
关于javascript如何切换window.location属性的路径名和重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7757304/