Nima Parsi
Nima Parsi

Reputation: 2110

How to remove the sites url from the link

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

Answers (3)

Lèse majesté
Lèse majesté

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

Giuseppe
Giuseppe

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

adeneo
adeneo

Reputation: 318192

var myHref = this.href.split('/');
var LinkTitle = myHref[myHref.length-1];

Upvotes: 3

Related Questions