I want to get the part of a URL after the last / and before the querystring.
So far I had the last part of the URL, but I wasn't able to remove the querystring from the URL.
The Javascript:
<script type='text/javascript'>
var url = window.location.href;
var array = url.split('/');
var lastsegment = array[array.length-1];
document.write(lastsegment);
</script>
The structure of the URL is like this:
http://www.example.com/search/People?&results=latest
I only need the People part. How could that be possible within the above Javascript?
Or is there any better way to do that?
Try using window.location.pathname instead of window.location.href. It gets only the text between the server name and the query string (which is "/search/People" in your case):
var last_part = window.location.pathname.split('/').pop();
You can read more about pathname here.
Read Window.location
window.location.pathname.split('/').pop();
.pop()
your code does the right thing, you could remove query strings by amending it a bit as;
var lastsegment = array[array.length-1];
if(lastsegment.indexOf('?'))
lastsegment = lastsegment.split('?')[0];
UPDATE:
To handle the case if there are no query string embedded at all.
If you want to parse an URL from another origin than window.location :
var test = 'http://www.example.com/search/People?&results=latest';
var parts = test.split('/');
lastPart = parts[parts.length-1].split('?')[0];
lastPart is now "People"
Related
I need to fetch the "NEED_THIS' from the below URL as separate strings.
'https://something--something.lightning.force.com/lightning/o/NEED_THIS/NEED_THIS?inContextOfRef=1.eyJ0eXBlIjoic3RhbmRhcmRfX29iamVjdFBhZ2UiLCJhdHRyaWJ1dGVzIjp7Im9iamVjdEFwaU5hbWUiOiJTdXBwb3J0X1JlcXVlc3RfX2MiLCJhY3Rpb25OYW1lIjoibGlzdCJ9LCJzdGF0ZSI6eyJmaWx0ZXJOYW1lIjoiUmVjZW50In19&count=1'
Is there any easy way to fetch this?
I already used decodeURIComponent(window.location.href.split('?')[0]); to get upto
https://something--something.lightning.force.com/lightning/o/NEED_THIS/NEED_THIS
First things first,
window.location.protocol = “https:”
window.location.host = “something--something.lightning.force.com”
window.location.pathname = “/lightning/o/NEED_THIS/NEED_THIS”
window.location.search = “?inContextOfRef=1.eyJ0eXBlIjoic3RhbmRhcmRfX29iamVjdFBhZ2UiLCJhdHRyaWJ1dGVzIjp7Im9iamVjdEFwaU5hbWUiOiJTdXBwb3J0X1JlcXVlc3RfX2MiLCJhY3Rpb25OYW1lIjoibGlzdCJ9LCJzdGF0ZSI6eyJmaWx0ZXJOYW1lIjoiUmVjZW50In19&count=1”
to break the pathname up, you can split it using:
var pathArray = window.location.pathname.split('/');
Then access the different parts by the parts of the array, like
var secondLevelLocationPartOne = pathArray[2];
var secondLevelLocationPartTwo = pathArray[3];
Then you can use it whereever you need it. for reference, click this link
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]);
i have an URL like the followin,
http://test.com/testing/test/12345
12345 is the id. I want to take this using query string. How to take this value in javascript?
try like this
http://test.com/testing/test/12345
var aarr = window.location.href.split('/');
//get last value
var id = aarr[aarr.length -1];
or just
var id = window.location.href.split('/').pop()
Use this :
document.location.href.split('/').pop()
Running it on this page yields : 22139563#22139563
Use this code:
var id = location.split('/').pop();
That's part of the path, not the query string... but you can access the page's URL using window.location.
The path is available at window.location.pathname which can be split up using forward slashes: window.location.pathname.split('/')
And then you can get the last item of the array: window.location.pathname.split('/').pop()
I would use substring, I think it's lighter than creating an array:
var id = window.location.href;
id = id.substring(id.lastIndexOf('/')+1);
I have this RegExp: var myReg = RegExp("https?://www.youtube.com/watch\?v=[^\"]+",""); to find the youtube link within a string. I then want to make the part of the string matching the RegExp a variable; lets say var url = "part of string matching the RegExp"
then I coudl do something like window.location = url; to redirect the browser directly to the video page. Thanks
You only have to access the first element of the result, if any:
var r = string.match(myReg);
if(r) var url = r[0];
Take care because is you dont find the url, the result will be a null value
This should do the trick:
myReg.exec(str);
var url = RegExp.lastMatch;
Udi
I am redirecting from some page to index.html. Page redirect to index by using following address in the address bar: http://localhost/index.html?page.html
How can I read the value after ? sign and insert it (page.html) into index.html file?
var url = window.location.href;
var params = url.split('?');
alert(params[1]);
window.location.search along with substring to remove the ?
More information can be found through the link regarding the property, but with regards to printing it on the page:
<script type="text/javascript">
var GET = window.location.search.substring(1);
document.write(GET);
</script>
Split() the document.URL by the lastIndexOf and parse it from there:
var url=document.URL;
var urls=url.substr(url.lastIndexOf('?')+1,url.length);
console.log(urls);
// urls will contain everything right of the ?
You could also extract it with a RegExp. Maybe not ideal but a solution nonetheless:
var url = location.href.match(/\?(.+)/)[1];