Remove some part of URL using javascript - 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]);

Related

Get values in URL path string JS

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

Javascript remove Ampersand "amp;" from the url

I have this url and I want to remove the "amp;" from it. Any ideas
http://localhost/projects/opencart/opencart230/upload/index.php?route=product/product&product_id=40
Consider working with URLs that do not feature HTML entities like &. If, for some reason, you cannot avoid them in your URLs, do the following to parse them:
const url = "http://localhost/projects/opencart/opencart230/upload/index.php?route=product/product&product_id=40";
const parseResult = new DOMParser().parseFromString(url, "text/html");
const parsedUrl = parseResult.documentElement.textContent;
console.log(parsedUrl);
You can use replace function
str = 'http://localhost/projects/opencart/opencart230/upload/index.php?route=product/product&product_id=40'
str.replace('&','&')
You might wanna use str.replace(/&/g, '&'); to replace all instead

JavaScript parameters from URL

var url = window.location.href;
result:
https://test.com/index.html?Event=90000002
how do I use Javascript to get 90000002 and put it in a variable?
You can use the .split() method
var data=url.split("=")[1]
Should return what you got after '='
Though this is not optimized way but you can still do like this
Use location object and it's search property.
location.search will return ?Event=90000002. Then use substring to get the value
var a = location.search;
console.log(a.substring(7,a.length))

how to get query string value using javascript

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

Split URL and Query Strings

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"

Categories

Resources