简体   繁体   中英

Remove some part of URL using javascript

I have a URL which will be something like below

http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg

I want URL which will be something like this

NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg

Here is what I get in variable

VSATSaving.PANAROMIC_120 = document.getElementById('ImgPanaromic120').src;

how to get that using javascript. Tried with lastIndexOf but it is not working

You can create a new URL object and use the pathname property to extract the data.

 const myUrl = new URL(document.getElementById('ImgPanaromic120').src); console.log(myUrl.pathname); 
 <img id="ImgPanaromic120" src="http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg"/> 

 var a = document.createElement('a'); a.href = 'http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg'; console.log(a.pathname); 

window.location.pathname将为您提供路径。

You can simply use window.location.pathname since this is a url.

For other strings, you can use indexOf , split , substring and many of the other string functions.

 //example using `split` var str = "http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg"; var paths = str.split("http://localhost:22306/") console.log(paths[1]); 

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