I am trying to get the parameters from a URL using js i have a url :
http://www.example.com?i=aGVsbG8gd29ybGQ=
i want to decode base64 ?i value and print decode value here using javascript
<input id="i" type="hidden" value="decode value" />
I am trying to get the parameters from a URL using js i have a url :
http://www.example.com?i=aGVsbG8gd29ybGQ=
i want to decode base64 ?i value and print decode value here using javascript
<input id="i" type="hidden" value="decode value" />
you could use the window.location API https://developer.mozilla.org/en-US/docs/Web/API/Location
console.log(window.location.search);
You will get as a response ?i=some-value
Yes you can get the value from url and and decode it.
Use below code
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
let x = atob(getUrlVars().i);
console.log(x); // hello world
document.getElementById('i').value = x;
To parse URL strings, modern browsers provide a class called URLSearchParams
and this is the easiest way to extract URL values.
You can create an instance of that class by passing it the search
property of window.location
(after removing the initial "?"), and then it will do all the work for you:
// eg. https://example.com/?name=Jonathan&age=18&i=aGVsbG8gd29ybGQ=
const params = new URLSearchParams(window.location.search.substring(1)); // remove "?"
const name = params.get("name"); // is the string "Jonathan"
const age = parseFloat(params.get("age")); // is the number 18
const i = whateverDecodingFunctionYouUse(params.get("i")); // decoded aGVsbG8gd29ybGQ
See: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/
Try this :
var parameterValue = atob(window.location.search.match(/(\?|&)i\=([^&]*)/)[2])
console.log(parameterValue);//"hello world"
document.getElementById('i').value=parameterValue;
This might help too : https://stackoverflow.com/a/26995016/957026