This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have a code but it is not running properly.
function QueryStringParam1(d) {
var vars = [], hash;
var q = document.URL.split('?')[0];
if (q != undefined) {
q = q.split('&');
for (var i = 0; i < q.length; i++) {
hash = q[i].split('=');
vars.push(hash[1]);
vars[hash[0]] = hash[1];
}
}
return vars[d];
}
Most likely you are trying to get query string value from this query. So you would be getting string having its value after '?' char.
So after split relevant string should be in 1 index not 0..
replace
var q = document.URL.split('?')[0];
by
var q = document.URL.split('?')[1];
Related
This question already has answers here:
How do I split a string into an array of characters? [duplicate]
(8 answers)
How to get character array from a string?
(14 answers)
Closed 1 year ago.
how to print as follows in javascript ?
if my input is "smart"
then my output could be "s,m,a,r,t"
I've tried by using this logic but i got the output as
s,m,a,r,t,
let a = " ";
str = userInput[0];
console.log(str);
for (let i = 0; i < str.length; i++) {
a = a + str[i] + ",";
}
console.log(a.trim());
You can use the split method in js
var str = "smart";
var res = str.split("").join();
console.log(res)
//if you want it to remain an array don't do the join
This question already has answers here:
Get HTML5 localStorage keys
(15 answers)
Closed 7 years ago.
I'm trying to print the names of all the keys stored in localStorage with each key in a separate line. This is my code:
function viewsaved(){
$('#saved').show();
var stuffsaved = Object.keys(localStorage);
var splitit = stuffsaved.split(',');
for (var i = 0 ; i < splitit.length ; i++ ){
$('#saved').append(splitit[i]+"<br>");
}
}
when I call the function, it does nothing.
How do you do this properly?
Object.keys returns an array, not a string. Just modify slightly:
var stuffsaved = Object.keys(localStorage);
for (var i = 0 ; i < stuffsaved.length ; i++ ) {
$('#saved').append(stuffsaved[i]+"<br>");
}
If you have or expect a lot of keys, I would suggest building the list in a temporary variable first to avoid frequent DOM update, for example:
var keys = Object.keys(localStorage);
var list = "";
for (var i = 0 ; i < keys.length ; i++ ) {
list += keys[i] + "<br>";
}
$('#saved').append(list);
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
I want to know why this procedure doesn't replace words
I have to do a procedure which reads a string and replace all word like this {{employee.Name}} into a value on the ticket's scope
var mySplitResult = Val.split(' ');
for (var i = 0; i < mySplitResult.length; i++) {
if (mySplitResult[i].match("{{") && mySplitResult[i].match(".")) {
var start = mySplitResult[i].lastIndexOf(".") + 1;
var end = mySplitResult[i].indexOf("}}");
var result = mySplitResult[i].substring(start, end);
for (var key in ticket.PNData) {
if (key == result) {
change.replace(mySplitResult[i], ticket.PNData[key]);
alert(change)
}
}
}
}
In JavaScript strings are immutable which means you must assign the result to a variable.
mySplitResult[i] = mychange.replace(mySplitResult[i], ticket.PNData[key]);
This question already has answers here:
Alternative to eval() javascript [duplicate]
(6 answers)
Closed 9 years ago.
I am making some common function where in I pass the array, name of field from array, value of field in array and return field name to return value as below
function arrayFilter(_array, findField, value, returnField) {
var temp = "_array[i]." + findField;
var retValue = "";
for (var i = 0; i < _array.length; i++) {
if (eval(temp) == value) {
return eval("_array[i]." + returnField);
}
}
}
But when I read on Internet I found that eval is not good, it can have string injection attack.
So somebody help on above.
Instead of:
return eval("_array[i]." + returnField);
Try:
return _array[i][returnField];
And also read this article.
You can use the square bracket notation for accessing properties when the value of your key is unknown.
function arrayFilter(_array, findField, value, returnField) {
var temp = _array[i][findField];
var retValue = "";
for (var i = 0; i < _array.length; i++) {
if (temp == value) {
return _array[i][returnField];
}
}
}
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
I want to grab the variables contained in the url after somebody logs into my Facebook app.
For example:
// window.location = .../#access_token=CTTG4fT3ci...&expires_in=5298
hash = window.location.hash;
data = PARSE(hash);
console.log(data['access_token'] + ', ' + data['expires_in']);
// returns: CAAG4fT3ci..., 5298
Is there a method or function similar to JSON.parse() that would convert "hash" into an array or object?
function PARSE (hash) {
var l, chunk, i = 0, out = {};
var chunks = hash.substr(1).split('&');
for ( l = chunks.length; i < l; i++ ) {
chunk = chunks[i].split('=');
out[ chunk[0] ] = chunk[1];
}
return out;
}
Here's the fiddle: http://jsfiddle.net/VwLJS/