1

我做了一个带有分页的过滤系统,我在尝试根据当前 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/

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

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

而不是这个:

http://localhost/posts/subscriptions/3

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

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

4

1 回答 1

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'
于 2013-05-25T16:34:05.497 回答