Get Only a Part of the Url with Javascript [duplicate] - javascript

This question already has answers here:
JavaScript - Get Portion of URL Path
(6 answers)
Closed 1 year ago.
hello My Url is as follows mysite.com/About how could I create a script capable of taking only the Url part after / ?

You can try this brother. Thank you.
let path = window.location.pathname;
path = path.split('/').filter((el, index) => index !== 0).join('/');
console.log(path);

You can easily use window.location.pathname to get the path.

Related

Get "id" part of outlook emai [duplicate]

This question already has answers here:
JavaScript - Get Portion of URL Path
(6 answers)
Closed 1 year ago.
Just a quick question, how would you go about getting the "id" from this URL:
https://outlook.live.com/mail/0/inbox/id/AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D
These URLs change alot, but i just want the message id.
Its not just a query param, so i can't just go and get it from there.
How would i do this?
You can just split the URL by / and get the 8th index, and if there is parameters, just split it by ? and get the first index
const url =
'https://outlook.live.com/mail/0/inbox/id/AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D?a=1&'
let url_split = url.split('/')
let id_with_parameter = url_split[7]
let id = id_with_parameter.split('?')[0]
console.log(id)
Output AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D
This probably has a lot of limitation and not the best way

Modify url and update with anather url using Javascript [duplicate]

This question already has answers here:
How to replace host part of a URL using javascript regex
(3 answers)
Closed 1 year ago.
I have below url
Input
url1 = https://www.example1.com/xyz/abc/123
I need modified above url and replace only domain to https://www.modifiedexample.com/
Output url2= https://www.modifiedexample.com/xyz/abc/123
This should work in all the browser
How to achieve this in Javasript in simple and efficient way
You can try URL API:
const url = new URL('https://www.example1.com/xyz/abc/123');
url.hostname = 'www.modifiedexample.com';
console.log(url.href); // https://www.modifiedexample.com/xyz/abc/123

Get the query string value from a URL using javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
I have a URL like
http://something.com?acess_token=ashv6786sdjfhjd
When I click on that link, how to get the access_token from that link using Javascript?
Try :
var Yoururl= "http://something.com?acess_token=ashv6786sdjfhjd";
var acessToken=Yoururl.split('?')[1].split('=')[1];
Working Fiddle
For url parameter see old so question from given link.
Get url parameter via jquery
If you want the value of the access_token you mean this:
ashv6786sdjfhjd
Then here's a quick solution. Hope it helps!
var str = "http://something.com?acess_token=ashv6786sdjfhjd.when";
var accessToken = str.split("=")[1].split(".")[0];
console.log(accessToken);

How to encode and set url as query string to other url in location.href? [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I have following url
http://www.example.com/p1/val1/p1/val2
Now I want to pass third parameter as url like '/path/to/file'
http://www.example.com/p1/val1/p1/val2/url/?????
How can I do that with combination of js and php?
After passing above, I would like to get value of url as '/path/to/file'
I am passing above link through js's location.href.
How to do that?
I am getting following error?
Not Found
The requested URL http://www.example.com/p1/val1/p1/val2/url//path/to/file was not found on this server.
why didn't use traditional query string?
http://www.example.com?p1[]=val1&p1[]=val2&url=<?php echo urlencode('/path/to/file'); ?>
I'm assuming you process your urls this way:
http://www.example.com/p1/val1/p2/val2/...
gets transformed into:
http://www.example.com?p1=val1&p2=val2&...
So, your poblem is that your url parameter receives, as value, either '' or just 'path'.
You could try this, with javascript
var url = 'http://www.example.com/p1/val/p1/val2';
var path = '/path/to/file';
var finalUrl = url + '/url/' + encodeURIComponent(path);

How do get URL in javascript? [duplicate]

This question already has answers here:
Get current URL with jQuery?
(33 answers)
Closed 8 years ago.
I want to get current page URL using javascript ,Any one have suggestions ?
Also i need to get the content from the hashtag For ex: xxxx.com/#21 i need to get that 21 in the javascript
Use next:
HREF = document.location.href
HASH = document.location.hash
http://www.w3schools.com/jsref/prop_loc_href.asp
http://www.w3schools.com/jsref/prop_loc_hash.asp
To get the entire current URL, use:
var currentURL = window.location;
Then to get the hash, without the # itself, use:
var trimmedHash = window.location.hash.substr(1);
// returns `21` from `xxxx.com/#21`
window.location.hash returns #21, and substr(1) returns all but the first character of that string, thus removing the #.
try:
window.location.hash // will result #21
window.location.hash.substr(1) // will result 21
hope that helps.

Categories

Resources