Let's say I have a page called https://randompagename.com, I know I can send GET parameters to this page using this syntax: https://randompagename.com/?parameter1="one"¶meter2="two".
I also know that on a Node.js web app I have an easy way of getting these parameters inside a variable. However, when I'm using pure frontend Javascript without Node.js, I usually solve this problem with something like:
const myURL = decodeURI(window.location.href)
This way, I discover that my page is https://randompagename.com/?parameter1="one"¶meter2="two" and then I can parse it excluding everything after the first = sign and then splitting everything on &. Well, even though this is functional I'm probably missing an easier way of solving this problem. How can I get GET parameters on a page without using any library?
You can use the URL object https://developer.mozilla.org/en-US/docs/Web/API/URL
If the URL of your page is https://example.com/?name=Jonathan%20Smith&age=18 you could parse out the name and age parameters using:
let searchString = (new URL(document.location)).search; //?name=Jonathan%20Smith&age=18
let params = (new URL(document.location)).searchParams;
let name = params.get('name'); // is the string "Jonathan Smith".
let age = parseInt(params.get('age')); // is the number 18
I'm writing a script on Google Script to interact with the Airtable's API.
To ask the API to return a list of records based in a query, they provide the URL to send the post request. However, for the query part of the Url they ask to translate special characters to HTML.
Straight to my question: Is there any cleaner way to code this?
My first solution was to create a dictionary for each character, then run a "replace" method to substitute the special characters to the relative ones on the dictionary.
Any person with a bit more knowledge in Javascript than me? Remembering that I'm running this on Google Scripts.
function urlEncoder(toEncode) {
var dictionary = {
'(':'(',')':')','`':'%60','~':'~','!':'!','#':'%40','#':'%23','^':'%5E','$':'%24','%':'%25','&':'%26',
'*':'*','-':'-','_':'_','=':'%3D','+':'%2B','[':'%5B',']':'%5D','{':'%7B','}':'%7D','\\':'%5C','|':'%7C',';':'%3B',':':':',
'\'':'\'','\"':'\"',',':'%2C','<':'%3C','.':'.','>':'%3E','/':'%2F','?':'%3F',' ':'+',
'à':'%C3%A0','À':'%C3%80','á':'%C3%A1','Á':'%C3%81','â':'%C3%A2','Â':'%C3%82','ä':'%C3%A4','Ä':'%C3%84','ã':'%C3%A3','Ã':'%C3%83',
'è':'%C3%A8','È':'%C3%88','é':'%C3%A9','É':'%C3%89','ê':'%C3%AA','Ê':'%C3%8A','ë':'%C3%AB','Ë':'%C3%8B',
'ì':'%C3%AC','Ì':'%C3%8C','í':'%C3%AD','Í':'%C3%8D',
'ò':'%C3%B2','Ò':'%C3%92','ó':'%C3%B3','Ó':'%C3%93','ô':'%C3%B4','Ô':'%C3%94','ö':'%C3%B6','Ö':'%C3%96','õ':'%C3%B5','Õ':'%C3%95',
'ù':'%C3%B9','Ù':'%C3%99','ú':'%C3%BA','Ú':'%C3%9A','û':'%C3%BB','Û':'%C3%9B','ü':'%C3%BC','Ü':'%C3%9C',
'ç':'%C3%A7','Ç':'%C3%87','ñ':'%C3%B1','Ñ':'%C3%91'
}
var result = toEncode.replace(/[^a-z0-9]/gi, m => dictionary[m])
Logger.log('URL Encoder:'+toEncode+'>>'+result)
return result
}
The string encoder provided by Airtable can be found here: https://codepen.io/airtable/full/rLKkYB
Thanks!
There is no need to make your own function for that, there's already a function called encodeURI in javascript:
var url = "my test.asp?name=ståle&car=saab";
var res = encodeURI(url);
// res = "my%20test.asp?name=st%C3%A5le&car=saab"
I have url "SampleProject/profile/aA12". How can I get the value of the id from my rewritten URL using javascript? I want to get the "aA12" value.
Im using htaccess rewrite to rewrite my URL. Im new in rewritting url's. Any help will be appreciated. More powers and thank you.
You can use regex.
Try
'SampleProject/profile/aA12'.match(/\SampleProject\/profile\/(\w+)/)
'SampleProject/profile/aA12/xxx'.match(/\SampleProject\/profile\/(\w+)/)
'aA12' will be matched in both cases.
There are going to be quite a few ways to achieve your goal with JavaScript. A simple solution could be something like this:
let myURL = "SampleProject/profile/aA12";
let result = myURL.split('/').pop();
// returns "aA12"
The .split('/') method is dividing your string up into an array using the / character, and .pop() is simply returning the last element of that array.
Hope this helps! If you were looking for more advanced matching, i.e. if you wanted to ignore a potential query string on the end of the URL parameter, you could use regular expressions.
Their is a many way that you can use to achieve the desired method i made you a code pen in this link
var url = "SampleProject/profile/aA12";
let res = url.split('/').pop();
console.log(res)
https://codepen.io/anon/pen/KQxNja
I'm expecting URLs in this for:
/user/username
but end users can also add whatever get parameters they want, like so:
/user/username?foo=bar
With that said, using AngularJS, what's the preferred way for me to get just the username (which appears after /user/) without anything else after it?
You should use the $location service and its .path() method, then use a regular split() and indexing.
I doubt there's a dedicated function for it but it seems easy enough to pull it out of the string with the query.
Get the path with $location.path() and then both of these does the job for you.
url.substring(6, (url.indexOf('?') != -1 ? url.indexOf('?') : url.length))
url.split('/')[2].split('?')[0]
Same question here: Is there a built-in way to get the current URL without any query parameters?
you can use window.location or $location service to get the path and then using split function
like this
var url = "/user/username?foo=bar";
var check = url.split('/user/');
var username = check[1].split('?');
console.log(username[0]);
you can find username by applying multiple split on your url. hope this is what you want.
I know just enough JS to get in trouble so please bear with me :)
I am using the WP-Properties WordPress plugin. When searching for properties it gives all results in a common search results page. I need to theme the search results page based on part of the search string so I need a body id.
Example of a search result url:
http://website.com/property/?wpp_search[pagination]=off&wpp_search[property_type]=oasis_park&wpp_search[lot_location]=Oceano+Lot&wpp_search[availability]=-1&wpp_search[phase]=-1&wpp_search[price][min]=-1
The part I want is what comes after: "wpp_search[property_type]"
In the above case it would be "oasis_park"
And this would then create a body tag of: <body id="oasis_park">
I tried to tweak the following code to get the specific part then have it write to the body tag but I can't get it to work in my situation: remove a part of a URL argument string in php
This will only work for this specific url, as you have not provided a general pattern for each url from which you will need to extract a substring:
var myString = "http://website.com/property/?wpp_search[pagination]=off&wpp_search[property_type]=oasis_park&wpp_search[lot_location]=Oceano+Lot&wpp_search[availability]=-1&wpp_search[phase]=-1&wpp_search[price][min]=-1";
var myVar = myString.slice(myString.indexOf("&") + 27, myString.indexOf("k"));
After you have identified a general pattern in every url you wish to check, you will then have to use a combination of substr(), slice() and indexOf() to get the substring you want.
Then, try
document.body.id = myVar;
or assign an id to body (e.g. "myID") then try this:
document.getElementById('myID').id = myVar;