How can I read a GET element from url via javascript? [duplicate] - javascript

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
My URL looks like this one:
http://www.something.hu/#/ask?kerdesPost=fdasdas%20ad%20asd%20ad%20asdas
I would like to get only fdasdas%20ad%20asd%20ad%20asdas or only kerdesPost=fdasdas%20ad%20asd%20ad%20asdas.
How can I do it via JavaScript? What is the shortest way?

You can try the following:
if((window.location.href).indexOf('?') != -1) {
var queryString = (window.location.href).substr((window.location.href).indexOf('?') + 1);
// "queryString" will now contain kerdesPost=fdasdas%20ad%20asd%20ad%20asdas
var value = (queryString.split('='))[1];
// "value" will now contain fdasdas%20ad%20asd%20ad%20asdas
value = decodeURIComponent(value);
// "value" will now contain fdasdas ad asd ad asdas (unescaped value)
}
JSFiddle
This should get you what you need.

Related

javascript problem replacing string occurrence [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 1 year ago.
i have the following code in my html page and as you can see i am trying to replace the occurrence of "state" with "item" and "number" with "count" in the string s
here is an example of the response text : [{"number":177,"state":"ABONDONNE"},{"number":132,"state":"ENCOURS"},{"number":6,"state":"GAGNE"},{"number":195,"state":"PERDU"},{"number":2,"state":"REPORTE"}]
here is my javascript code:
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
var s = xhttp.responseText;
s.replaceAll("state","item");
s.replaceAll("number","count");
var jsonArray = JSON.parse(s);
console.log(s);
}
xhttp.open("GET","http://localhost:8080/plot");
xhttp.send();
my problem is that after this code i get the same response text in the console:
[{"number":177,"state":"ABONDONNE"},{"number":132,"state":"ENCOURS"},{"number":6,"state":"GAGNE"},{"number":195,"state":"PERDU"},{"number":2,"state":"REPORTE"}]
what i am missing?
You need to assign a new string returned by replaceAll function.
let updatedString = s.replaceAll("state","item")
You don't assign functions's result anywhere. Change to:
s = s.replaceAll("state","item");
s = s.replaceAll("number","count");

javascript: how to save string substitution in another string? [duplicate]

This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 1 year ago.
I have a string and array
var s1 = "hello %s, i am %d years old";
var s2 =[John,24];
Expected result:
s3 = hello John i am 24 years old
I want to save the output into another string.
I'm able to display output in console
console.log(s1, ...s2)
But not able to store in other string.
I tried many things like:
var s3 = s1.format(...s2)
Any suggestions?
Unfortunately there is no string formatter available in JS, you'd have to write that manually like
let i = 0;
s3 = s1.replace(/%(s|d)/g, (_, type) => s2[i++]);
You could use the template string from ES6:
let anotherString = `Hello ${s2[0]}, I am ${s2[1]} years old`;
You could use this too:
String.prototype.format = function() {
a = this;
for (k in arguments) {
a = a.replace("{" + k + "}", arguments[k])
}
return a
}
Usage:
let anotherString = '{0} {1}!'.format('Hello', 'Word');
Than those solutions I dont see any other way to do that.

Issue in URL Splitting [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have write following code to split the URL
var href = document.URL;
var GlobalBoomGuid = href.split("=");
var optionid = GlobalBoomGuid[1];
If i have URL like this : www.mydomain.com?optionid=655 It's giving me "655" .
But in current scenario my URL is www.mydomain.com?optionid=655#&ui-state=dialog and it's returning me 655#&ui-state
Now i want only id . How should i do that ?
Please if you dont like the question don't mark as a Negative
Thanx in Advance :)
This gets your result
var href = document.URL;
var foo = href.split("=")[1];
var GlobalBoomGuid = foo.split("#")[0];
var optionid = GlobalBoomGuid;
fiddle

How to make a javaScript variable equal to something or something else? [duplicate]

This question already has answers here:
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 8 years ago.
Trying to add randomness to which image is displayed. Issue with saying calling and or statement.
var image = url3 | url2 | url1;
buildImage(image);
function buildImage(imagesrc) {
var img = document.createElement('img');
img.src = imagesrc;
document.getElementById('content').appendChild(img);
var images = [url3, url2, url1];
var image = images[(Math.random() * images.length)|0];
The "or" operators are not probabilistic "or" that will return one value or another. They are very specific: || will return the first value unless it's falsy, in which case it returns the second; and | converts both arguments to integers and applies the same operation to each individual binary digit.
Instead of using separate variables, make a urls array:
var urls = [
"http://example.com/img1.jpg",
"http://example.com/img2.jpg",
"http://example.com/img3.jpg"
];
You can then select a random URL by doing:
var url = urls[Math.floor(Math.random() * urls.length)];
And call buildImage:
buildImage(url);

getting parameter from url [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Get escaped URL parameter
(19 answers)
Closed 9 years ago.
I have this url index.html#secondPage?name=the%20second%20page
I want to get the value of name ("the second page") using javascript and jquery
thanks
You can use the code from the answers to this question. The only difference is that you want to parse the location.hash instead of location.search so just change that in whichever answer you choose to go with.
You'll also need to use substr to delete the leading # like:
var hash = window.location.hash.substr(1);
Here is the code from my answer to the question I linked to, with the modification:
function get_query(){
var url = location.hash.substr(1);
var qs = url.substring(url.indexOf('?') + 1).split('&');
for(var i = 0, result = {}; i < qs.length; i++){
qs[i] = qs[i].split('=');
result[qs[i][0]] = decodeURIComponent(qs[i][1]);
}
return result;
}
You can use it like:
var secondPage = get_query()['name']; // 'the second page'
Try like below, It will help you
Fiddle : http://jsfiddle.net/RYh7U/144/
Javascript :
function GetURLValue (sKey) {
return unescape("index.html#secondPage?name=the%20second%20page".replace(new RegExp("^(?:.*[&\\?]" + escape(sKey).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}
alert(GetURLValue("name"));

Categories