How to get the URL parameter using Javascript in raw format - javascript

So I currently get passed a query parameter in one of my URLs on my web application. The query paramater looks like
https://localhost:8080/home?code=v%5E1.1%23i%5E1%23p%5E3%23I%5E3%23f%5E0%23r%5E1%23t%5EUl41XzEwOkFGNjA3MEMzMzRGOThFRTgwMkMxRUVGQjhGRUZEOTREXzJfMSNFXjI2MA%3D%3D&expires_in=299
I am currently parsing that code parameter using
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code')
const expires_in = urlParams.get('expires_in')
However the parsed URLSearchParams looks like
v^1.1#i^1#p^3#I^3#f^0#r^1#t^Ul41XzEwOkFGNjA3MEMzMzRGOThFRTgwMkMxRUVGQjhGRUZEOTREXzJfMSNFXjI2MA==
I want to use the raw code parameter seen in the URL. Is there a way to get this?

Related

How to pass URL query string to website header?

I have this type of URL:
www.domain.com/postname?emailid=somethingsomething
I must place this part:
somethingsomething
inside WP header, more accurate inside JS snippet, as follows:
<script>
window.HashedEmail = 'somethingsomething';
</script>
EDIT: Suggested answer shows how to extract particular data but not how to place extracted data into header, for example - how to manipulate with extracted dataset
Maybe I missed something but this looks like a solution for me
const urlSearchParams = new URLSearchParams(window.location.search);
const emailid = urlSearchParams.get('emailid');
window.HashedEmail = emailid;

Retrive image with URL paramter and javascript

Sadly, I have not enough experience in JS to do this.
I want to call the Google chart API with my URL paramter as data.
Here I used Javascript to store the URL paramter 'voucher' in a variable from my Website:
mywebsite.com/core/voucher/?voucher=123
This is the code I use:
<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const voucher= urlParams.get('voucher');
//voucher = 123
</script>
<img id="qr" src="https://chart.apis.google.com/chart?cht=qr&chs=400x400&chl=1"/>
Now I need the 'voucher' variable to call the Google chart URL with my variable as the chl URL paramter. How can i archive this?

Update query strings onChange - reactjs

I have a url with query strings which looks like this
http://localhost:3000/collection?page=1&limit=15&filter=%27%27&filterInitDate=2019/11/11&filterEndDate=2019/11/11
I want to update just the filterInitDate when i select a date from my date input. I know i can update the url simply by using this.props.history.push but how can i just change/update the filterInitDate when i change the date input
ideally something like this
history.push(`/collection?filterInitDate=${startDate}`)
You can use the URL and URLSearchParams classes for that, here is an example:
const urlStr = 'http://localhost:3000/collection?page=1&limit=15&filter=%27%27&filterInitDate=2019/11/11&filterEndDate=2019/11/11';
const url = new URL(urlStr);
url.searchParams.set("filterInitDate", "2019/11/12");
console.log(url.toString());
And an example specific to react router will look like this:
const query = new URLSearchParams(history.location.search);
query.set("filterInitDate", startDate);
history.replace({
search: query.toString()
});

Django get last GET parameter

I've been working with Django for a few months now so I'm still new at it.
I want to get the last GET parameter from the URL. Here is and example of the URL:
example.com?q=Something&filter1=Test&filter1=New&filter2=Web&filter3=Mine
Is there a way to get the last inserted GET parameter with django? It could be filter1, filter2 or filter3..
Maybe there is a way to do this after the initial refresh with javascript/jQuery?
Thanks!
You can try to parse url parameters yourself. For example:
Python/Django
from urlparse import urlparse, parse_qsl
full_url = ''.join([request.path, '?', request.META['QUERY_STRING']])
#example.com?q=Something&filter1=Test&filter1=New&filter2=Web&filter3=Mine
parameters = parse_qsl(urlparse(full_url)[4])
#[(u'q', u'Something'), (u'filter1', u'Test'), (u'filter1', u'New'), (u'filter2', u'Web'), (u'filter3', u'Mine')]
last_parameter = parameters[-1]
#(u'filter3', u'Mine')
Javascript
var params = window.location.search.split("&");
//["?q=Something", "filter1=Test", "filter1=New", "filter2=Web", "filter3=Mine"]
var last_param = params[params.length-1].replace("?","").split("=");
//["filter3", "Mine"]
This example do not use jQuery and provides basic knowledge of url parsing. There are a lot of libraries, that can do it for you.

Passing an id value into a URL

My company hosts user created surveys on our server. When they are uploaded, they are given a key number as an identifier. I am trying to create a facebook app that people can post a simple survey to and distribute. I can set the canvas URL to the default URL of our server, but I need to pass that key to the query string at the end of the app URL.
<input type="hidden" id="SurveyKey" name="SurveyKey" value="130633791306">
so, the end link needs to be apps.facebook.com/myappname/130633791306
or apps.facebook.com/myappname/SurveyKey value
I am very new to JavaScript and didn't know if there was some get function that could just pull that from the source code and pass it into a new URL. I'm sure this is something easy, but as I am not sure how to word my question, my search result is coming up with a lot of unrelated material.
The URLs for our surveys look like this:
http://www.snapsurveys.com/swh/surveylogin.asp?k=130633791306
where k is a unique value for every survey. I want to be able to pull that value from the source code and pass it into the URL of my facebook app (which has the canvas URL set as our URL). So, it would look like apps.facebook.com/appname/k=VALUE_HERE
To get the query string in JavaScript you could use a code snipet like this:
function querySt(ji) {
hu = window.location.search.substring(1);
gy = hu.split("&");
for (i=0;i<gy.length;i++) {
ft = gy[i].split("=");
if (ft[0] == ji) {
return ft[1];
}
}
}
Then you just define a variable to store the key, ie
var surveyKey = querySt("k");
Now you can use the surveyKey anywhere, so for example:
var url = "http://apps.facebook.com/appname/k=" + surveyKey;
http://ilovethecode.com/Javascript/Javascript-Tutorials-How_To-Easy/Get_Query_String_Using_Javascript.shtml

Categories

Resources