javascript get querystring [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
obtaining querystring from current url in javascript?
I was to get everything after the question mark in the url. Ive seen a way to do it that does not require a function but cannot find it for the life of me.
url
fsdhfbsdfj/index.html?hello
get
hello

Use
var query = window.location.search;
If you want to get rid of the starting "?", use
var query = window.location.search.slice(1);
The properties of the location object are described here.

var querystring=window.location.search.substring(1);

Your title says "get querystring", but your question says "get everything after the question mark" which is something different and can include both a querystring and a hash.
I assume that when you say "the" url you are talking about the current Web page.
To get the querystring:
window.location.search
To get everything after the first ?:
window.location.href.split("?").slice(1).join("?");

You can find the query string in window.location.search

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

how to get url parameters to appear on an html page WITHOUT php [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 2 years ago.
I am developing a site in HTML (not PHP) and am trying to get url parameters to display on an appointment confirmation page. The appointment confirmation info is successfully passed to the URL parameters. But I can't figure out how to get them to appear on the page.
For example:
?name=John&date=Nov-26&Time=4pm
to appear on the html page as:
Name: John
Date: Nov 26
Time: 4pm
I have watched tutorials on youtube, followed instructions and copied code from examples on stackoverflow, but the results are a complete failure. Nothing appears on the pages at all.
If you reply, could you please provide the full code as I do do not know javascript and its exceedingly difficult for me to try to figure out where added options get inserted into any base coding you provide. For instance, if there is code with "var" in the option, does that replace the "var" that is in the base code offered, or does it get added to it? If it gets added to it, where does it go? Does it go after the semi-colon, or do I need to start a new curly bracket after the closed curly bracket?
Thanks in advance.
Try this - I am using URLSearchParams to extract the values from the URL and template literals to create the string
Change "?name=John&date=Nov-26&Time=4pm" to location.search on your site
<footer id="footer"></footer>
<script>
const srch = "?name=John&date=Nov-26&Time=4pm"; // location.search;
const parms = new URLSearchParams(srch);
document.getElementById("footer").innerHTML += `Name: <span id="name">${parms.get("name")}</span>
Date: <span id="date">${parms.get("date").replace(/-/g," ")}</span>
Time: <span id="time">${parms.get("Time")}</span>`;
</script>

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 I read #2 at the end of a URL? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can you check for a #hash in a URL using JavaScript?
Currently I am generating Urls that look like this:
InspectionPhotos.aspx?inspectionId=10001649#/2
The #2 is for a photogallery plugin, and this would mean go the second photo.
I would like to show a div only if there is a #/[anynumber] but if its just
InspectionPhotos.aspx?inspectionId=10001649
then not show anything.
How could I do this check? Either asp.net on pageload or a client side javascript would be fine.
Thanks.
You can't do this in server side, because the hash is not sent to the server, to get this value with javascript is simple:
var hash = window.location.hash;
if (hash){
//use the hash value.
}
JavaScript: window.location.hash will comeback with #/2 from your example.

Categories

Resources