【发布时间】:2014-08-08 04:47:16
【问题描述】:
我想在 404 页面上添加修改后的链接,因为我将永久链接结构从 domain.tld/category/title 移动到了 domain.tld/title。 现在,当访问者找到指向 domain.tld/category/title 的旧链接时,她/他将看到 404 页面,并且应该有指向 domain.tld/title 的动态链接。 如果 window.location.href 包含五个“/”,则应该生成此链接(因为http://www.domain.tld/category/title/ 有五个“/”,而我的新永久链接结构不会有五个“/”而只有四个“/”。 我知道我可以用此代码替换(删除)类别部分:
function geturl(){
var url = window.location.href;
alert(url.replace('/category/', '/'));
}
问题在于我必须定义一个静态类别名称,但它可以是任何名称。 window.location.href中有五个“/”时如何动态去掉/category/部分?
【问题讨论】:
-
听起来像是应该由网络服务器重定向的东西..
-
丑陋的解决方案:url.replace(/\/\/(.+)\/(.+)\/(.+)\//,"//$1/$3");
标签: javascript