Reputation: 2110
I'm using ajax for my blog and need to change the title when the next page is loaded.
I'm trying to do this by getting the url from the next's link and add it to the title. Now I just need to remove the site's url for example from this url: www.mysite.com/page/2
I only need to target after the last slash Only: 2
By using this after clicking on the next page I target the whole url:
var LinkTitle = jQuery(this).attr('href');
Any ideas? Thanks!
Upvotes: 0
Views: 92
Reputation: 8045
This should also work:
url = url.substr(url.lastIndexOf('/') + 1);
There are obviously a lot of ways to do it (regex, replace()
, split()
, etc.). I suggest going on jsperf and seeing which is the quickest on most browsers, and doing that. And maybe you can report those results on here so future people who visit the question can take advantage of that info as well.
Upvotes: 2
Reputation: 597
You may use split() function to obtain an array and get its last element.
arr = LinkTitle.split("/");
yourTitle = arr[arr.length-1];
Upvotes: 2
Reputation: 318192
var myHref = this.href.split('/');
var LinkTitle = myHref[myHref.length-1];
Upvotes: 3