重定向Javascript中包含的URL路径

Redirect URL paths included with Javascript

本文关键字:URL 路径 包含 Javascript 重定向      更新时间:2024-07-01

我看到了几个关于通过javascript重定向到页面的问题,这很好,但所有这些问题都只是重定向到一个基本url。我想包括一个可变路径。比如说,我有http://www.glas.com/my/url/path,我想用window.location.replace()只替换glas部分,并将其更改为glass。我是JavaScript的初学者,如果有任何帮助,我将不胜感激。

这就是我迄今为止所尝试的:

var path = ''; // this is where I'm stuck
window.location.replace("http://www.glass.com/" + path);

其中path变量将解析当前URL中/之后的任何内容。我试过查看JavaScript的matchregexp,但它们看起来都很混乱。

尝试

window.location = window.location.href.replace('glas', 'glass');

您可以执行以下操作:

window.location.href = window.location.href.replace('glas','glass');