How to pass URL query string to website header? - javascript

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;

Related

Angular: How to get copied URL from clipboard?

So this is how I extract the copied text from clipboard:
const data = event.clipboardData.getData('text/html');
But this code does not work if I copied a URL. The data will be "" empty. Not sure why is it different with a normal text.
How do I get the copied URL?
EDIT
The event I got is from paste event:
paste(event: ClipboardEvent) {
const data = event.clipboardData.getData('text/html');
}
From this HTML element (I am using Angular):
<div
id="content"
contentEditable="true"
(paste)="paste($event)"
></div>
Not sure what is the event in your code, but you can get data from clipboard anywhere in your browser via Clipboard API
https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText
const copiedData = await navigator.clipboard.readText()
So from what I've read from this Codepen, the code takes the textContent from the quote blocks and then put them all in the clipboard
handleCopyClick.addEventListener('click', () => {
let text = quoteText.textContent;
let author = quoteAuthor.textContent;
navigator.clipboard.writeText(`${text} ${author}`);
});
Adapting to your case though, the code would look something like this:
paste(event: ClipboardEvent) {
let url = urlBlock.textContent;
navigator['clipboard'].writeText(`${url}`)
}
Props to this FreeCodeCamp article for the inspiration.
This one SO post might also answer some of your questions too.
Actually I found that the clipboardData sometimes hold different types of string. In my code:
const data = event.clipboardData.getData('text/html');
This only extract text/html type.
There are other types like text/plain. So this is my solution:
let data = event.clipboardData.getData('text/html');
data = data && data.length > 0 ? data : event.clipboardData.getData('text/plain');
If text/html is empty, then get text/plain data.

How to get the URL parameter using Javascript in raw format

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?

Query string with fetch javascript

I'm working on a project and i want to define url object that will be passed to fetch ex.
https://t1.testing.com/test/api/v1/blog?pagingindex=0&pagingresults=10
const url_object = {
url: `https://t1.testing.com/test/api/v1/blog`,
url_params: {
method: "POST",
pagingindex: 0,
pagingresults: 10
}
};
and when i call fetch(url_object.url, url_object.url_params) i get an error. How can i incorporate this into fetch? So i need to allow user to define method and query string that will be passed. Thanks in advance!
You can have your cake and eat it too -- easily convert dictionary style objects to query parameters with no fuss:
var url = new URL('https://example.com/');
url.search = new URLSearchParams({blah: 'lalala', rawr: 'arwrar'});
console.log(url.toString()); // https://example.com/?blah=lalala&rawr=arwrar
The object you have labeled url_params is described by the MDN documentation as:
An options object containing any custom settings that you want to apply to the request.
It then goes on to list the properties you can include there.
pagingindex and pagingresults are not among them.
The query string is part of the URL. If you want to put data there, then put it in the URL.
const url_object = {
url: `https://t1.testing.com/test/api/v1/blog?pagingindex=0&pagingresults=10`,
url_params: {
method: "POST"
}
};
You may wish to use the URL object to construct the URL (it will handle escaping for you).

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.

Parameter retrieval for HTTP PUT requests under IIS5.1 and ASP-classic?

I'm trying to implement a REST interface under IIS5.1/ASP-classic (XP-Pro development box). So far, I cannot find the incantation required to retrieve request content variables under the PUT HTTP method.
With a request like:
PUT http://localhost/rest/default.asp?/record/1336
Department=Sales&Name=Jonathan%20Doe%203548
how do I read Department and Name values into my ASP code?
Request.Form appears to only support POST requests. Request.ServerVariables only gets me to header information. Request.QueryString doesn't get me to the content either...
Based on the replies from AnthonyWJones and ars I went down the BinaryRead path and came up with the first attempt below:
var byteCount = Request.TotalBytes;
var binContent = Request.BinaryRead(byteCount);
var myBinary = '';
var rst = Server.CreateObject('ADODB.Recordset');
rst.Fields.Append('myBinary', 201, byteCount);
rst.Open();
rst.AddNew();
rst('myBinary').AppendChunk(binContent);
rst.update();
var binaryString = rst('myBinary');
var contentString = binaryString.Value;
var parameters = {};
var pairs = HtmlDecode(contentString).split(/&/);
for(var pair in pairs) {
var param = pairs[pair].split(/=/);
parameters[param[0]] = decodeURI(param[1]);
}
This blog post by David Wang, and an HtmlDecode() function taken from Andy Oakley at blogs.msdn.com, also helped a lot.
Doing this splitting and escaping by hand, I'm sure there are a 1001 bugs in here but at least I'm moving again. Thanks.
Unfortunately ASP predates the REST concept by quite some years.
If you are going RESTFull then I would consider not using url encoded form data. Use XML instead. You will be able to accept an XML entity body with:-
Dim xml : Set xml = CreateObject("MSXML2.DOMDocument.3.0")
xml.async = false
xml.Load Request
Otherwise you will need to use BinaryRead on the Request object and then laboriously convert the byte array to text then parse the url encoding yourself along with decoding the escape sequences.
Try using the BinaryRead method in the Request object:
http://www.w3schools.com/ASP/met_binaryread.asp
Other options are to write an ASP server component or ISAPI filter:
http://www.codeproject.com/KB/asp/cookie.aspx

Categories

Resources