Nitish Kumar
Nitish Kumar

Reputation: 6276

How to remove domain name from the src URL attribute in Jquery

I'm fetching img src attribute into a variable, while doing so I'm getting the complete url of the image, i want to remove domain name from the url

var imgurl = "http://nitseditor.dev/img/home/bg.jpg";

I want to have only img/home/bg.jpg. How can I achieve it?

Upvotes: 7

Views: 8537

Answers (3)

Pranav C Balan
Pranav C Balan

Reputation: 115222

Get substring by getting the index of third /.

var imgurl = "http://nitseditor.dev/img/home/bg.jpg";

console.log(
  imgurl.substr(imgurl.indexOf('/', 7) + 1)
);

Upvotes: 5

Văn Tuấn Phạm
Văn Tuấn Phạm

Reputation: 659

url = url.replace(/^.*\/\/[^\/]+/, '')

Upvotes: 7

Tushar
Tushar

Reputation: 87203

You can use URL constructor.

This is an experimental technology

var url = new URL(imgurl);
console.log(url.pathname);

var imgurl = "http://nitseditor.dev/img/home/bg.jpg";
var url = new URL(imgurl);
console.log(url.pathname);

Browser Support

Upvotes: 14

Related Questions