【问题标题】:Update last path on the URL with javascript使用 javascript 更新 URL 上的最后一个路径
【发布时间】:2012-10-10 07:12:52
【问题描述】:

我正在做一个带有分页的过滤系统,但我在尝试根据当前 URL 使用 Javascript 重定向时遇到了麻烦。

我可以拥有这种类型的 URL,其中最后一个数字是过滤器:

http://localhost/posts/yourPosts/1
http://localhost/posts/subscriptions/3

并且在每种类型中,第2页都会以这种方式显示:

http://localhost/posts/yourPosts/1/page:2?url=posts%2FyourPosts
http://localhost/posts/subscriptions/3/page:2?url=posts%2FSubscriptions

我目前正在使用:

window.location.href = currentURL + '/' + encodeURI($(this).val());

其中 $(this).val 是要使用的过滤器的编号。

问题是当我在这个 URL 中时它运行良好:

http://localhost/posts/yourPosts/
http://localhost/posts/subscriptions/

但是当我在第二页并且我想显示过滤器编号 3(比如说,汽车而不是动物)时,它没有。它显示了这一点:

http://localhost/posts/subscriptions/2/page:2?url=posts%2FyourPosts/3

而不是这个:

http://localhost/posts/subscriptions/3

有没有简单的处理URL的方法来解决这类问题?

我也不能使用绝对路径,因为 URL 并不完全相同。那么...有什么办法可以更改 URL 的最后一个 PATH 吗?

【问题讨论】:

    标签: javascript url url-rewriting path


    【解决方案1】:

    快速且有点脏:

    function fixmybadfilterurl(mybadurl)
    {
        var parts = mybadurl.split('?');
        var subpart2 = parts[parts.length-1].split('/');
        var subpart1 = parts[0].split('/');
        subpart1.pop();
        subpart1.pop();
        return(subpart1.join('/')+'/'+subpart2[subpart2.length-1]);
    }
    

    所以

    fixmybadfilterurl('http://localhost/posts/subscriptions/2/page:2?url=posts%2FyourPosts/3');
    

    会回来

    'http://localhost/posts/subscriptions/3'
    

    【讨论】:

      猜你喜欢
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      相关资源
      最近更新 更多