1

I doing a filter system with pagination and i am in a trouble trying to redirect with Javascript depending on the current URL.

I can have this types of URLs where the last number is a filter:

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

And inside each type, page 2 will be shown in this way:

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

I am currently using:

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

Where $(this).val is the number of the filter to use.

The problem is that when i am in this URLs it works well:

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

But when i am in the 2nd page and i want to show the filter number 3 (lets say, cars instead of animals), it doesn't. It show this:

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

Instead of this:

http://localhost/posts/subscriptions/3

Is there any simple way to deal with URLs to solve this kind of problem?

I can not either use an absolute path because the URLs are not all the same. So... is there any way to change the last PATH of a URL?

1 Answer 1

1

quick and somewhat dirty:

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]);
}

so

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

will return

'http://localhost/posts/subscriptions/3'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.