Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 200
Nitish Kumar Avatar asked Aug 16 '16 08:08

Nitish Kumar


2 Answers

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

like image 174
Tushar Avatar answered Nov 01 '22 23:11

Tushar


url = url.replace(/^.*\/\/[^\/]+/, '')
like image 6
Văn Tuấn Phạm Avatar answered Nov 01 '22 23:11

Văn Tuấn Phạm