How to get specific information from a url? - javascript

I would like to take only the part using javascript
cdf28fbc167d0fa6bdbf2928f1a18235a5d2ab24659455f0108dc3bcdcbe5608
from the url:
http://localhost:3000/resetPassword/cdf28fbc167d0fa6bdbf2928f1a18235a5d2ab24650855cbe01c6b01
How can I do it?
I tried to use location but without success

It can be achived by using lastIndexOf and substring:
const url = 'http://localhost:3000/resetPassword/cdf28fbc167d0fa6bdbf2928f1a18235a5d2ab24650855cbe01c6b01'
console.log(url.substring(url.lastIndexOf('/') + 1))

const url = "http://localhost:3000/resetPassword/cdf28fbc167d0fa6bdbf2928f1a18235a5d2ab24650855cbe01c6b01";
console.log(url.split("/")[4])

You can use:
window.location.href.split("/").slice(-1)[0]

Related

Add with javascript a image extension to urls

I have some urls and I want to add the string ".webp" in the alphanumeric id just before the parameter &size
http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g&size=g&v=20210314224102
http://r.rp-static.pre/r/dsn-icon?dsn=caucp0d8ig8o&size=g&v=20210309074045
http://r.rp-static.pre/r/dsn-icon?dsn=9guq9t6e5fs1&size=g&v=20210318114201
I only get these strings so I have to add ".webp" somehow with javascript.
I want this result:
http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g.webp&size=g&v=20210314224102
http://r.rp-static.pre/r/dsn-icon?dsn=caucp0d8ig8o.webp&size=g&v=20210309074045
http://r.rp-static.pre/r/dsn-icon?dsn=9guq9t6e5fs1.webp&size=g&v=20210318114201
how can I do it? Thanks
let temp = "http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g&size=g&v=20210314224102"
console.log(temp.slice(0, temp.indexOf("&size")) + ".webp" + temp.slice(temp.indexOf("&size")))
You can store the url first in a variable and then use slice to update the url.
console.log('http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g&size=g&v=20210314224102'.replace(/&size/, '.webap&size'))
let a ='http://r.rp-static.pre/r/dsn-icondsn=xjfhob38jg8g&size=g&v=20210314224102'
//your url
let url = a.replace('&size', '.webp&size');
//new url 'http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g.webp&size=g&v=20210314224102'

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 will give you path.
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]);

javascript getting substrings in URL

In my website I use:
- www.example.com/es/index.html
- www.example.com/en/index.html
- www.example.com/ch/index.html
How can I easily get the value language? (es or en or ch)
language = url.split("/")[1];
Where url is the URL you are parsing. Maybe use 'location' to get it.
You can use:
var url = 'www.example.com/es/index.html'
var lang = url.split('/')[1]
can use var lang = location.pathname.split('/')[1]
this assumes that the language directory is always right after .com
While all those
str.split("/")[1]
solutions are good I want to suggest a regex (if only for completeness sake).
Using
str = "www.example.com/es/index.html";
str.match(/www\.example\.com\/(.+)\//)[1]
works aswell and furthermore it also matches correctly for strings like:
"http://www.example.com/es/index.html"
fiddle: http://jsfiddle.net/zpmvrv2a/
As u_mulder suggested, you can use document.location like so:
document.location.href.split("/")[1];

How to remove "/" in JavaScript location.pathname return value

I request the javascript guru masters again!
Okay this is probably simple, blowing my mind atm.
Example: http://example.com/page.php
var path = location.pathname;
Returns: /page.php
I would like it to not include the first / so it would return page.php
Thank you guru masters!
You just need to use one of the several substring functions.
var path = location.pathname.substr(1);
Update:
substr is now a deprecated method, so you should use slice instead.
var path = location.pathname.slice(1);
var path = location.pathname.substring(1);
You can use replace() function also, but it replaces the all found forward slashes in path.
var path = location.pathName.replace("/","");

How to modify URL by JavaScript or jQuery

I want to remove the string code=hads1328fas& on my URL, so that
BEFORE
http://example.com/main/index.php?code=hads1328fas&store=food#home
AFTER
http://example.com/main/index.php?store=food#home
However, the string code=hads1328fas& may not always the same, so the code below won't works in this case.
var url = window.location.href;
url.replace('code=hads1328fas&')
Is there any way that is possible for the case?
Thanks
Use regular expressions:
url = "http://example.com/main/index.php?code=hads1328fas&store=food#home";
url = url.replace(/code=[^&]+&/,'');
After this, url will contain
http://example.com/main/index.php?store=food#home

Categories

Resources