简体   繁体   中英

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM