Silas2
asked on
Remove quotes JScript
I'm getting strings in my javascript which are really a property arrays:
How do i turn the strings into property arrays?
"{ name: 'StatementItemID', index: 'ItemID', width: 24, sortable: false }"
"{ name: 'UIDesChargeDate', index: 'UIDesChargeDate', width: 60 , align: 'center', sortable: false}"
don't ask why the quotes are there...but when i try to turn that into an array of the contained properties, i just get an array of complete text lines (inc the "{}"'s), which is messing the the pluggin.How do i turn the strings into property arrays?
I agree it should probably not be done but see if this helps you:
<html>
<head>
<script type="text/javascript">
var a = Array();
a[a.length] = "{ name: 'StatementItemID', index: 'ItemID', width: 24, sortable: false }";
a[a.length] = "{ name: 'UIDesChargeDate', index: 'UIDesChargeDate', width: 60 , align: 'center', sortable: false}";
for (var k in a) {
eval('a[k]='+a[k]+';');
}
function writeAll() {
for (var k in a) {
document.write('a['+k+'] = {<br>');
writeProps(a[k]);
document.write('}<br>');
}
}
function writeProps(aa) {
for (var pp in aa) {
document.write(' '+pp+' = '+aa[pp]+'<br>');
}
}
</script>
</head>
<body>
<script type="text/javascript">
writeAll();
</script>
</body>
</html>
ASKER
This is the reason the quotes are there:
https://www.experts-exchange.com/questions/28087620/Javascript-Escape-characters.html
I had to use decodeURI on an escaped embedded (from server script) string.
If you can see a way of removing the quotes with the decodeURI then i'm all ears...
https://www.experts-exchange.com/questions/28087620/Javascript-Escape-characters.html
I had to use decodeURI on an escaped embedded (from server script) string.
If you can see a way of removing the quotes with the decodeURI then i'm all ears...
How about directly using eval() on that output?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
That's great! Thanks, that is working. I sort of remember years ago reading that eval in jscript was a bit of a no-no. Is this an acceptable usage?
(I'm afraid, i don't really understand what it's doing, how does setting a variable to itself remove the string/quotes ("x=" + x) ?)
(I'm afraid, i don't really understand what it's doing, how does setting a variable to itself remove the string/quotes ("x=" + x) ?)
It _is_ a bit of a no no in a lot of situations due to performance, scoping, even security. But an alternative in this case I can't think of.
The trick is, it executes the resulting string as a statement of its own. So the resulting variable doesn't have the quotes because if you have
The trick is, it executes the resulting string as a statement of its own. So the resulting variable doesn't have the quotes because if you have
var str = "some string";
then the quotes are not actually part of the content of the variable, so they won't be after eval'ing like this.
don't use eval especially here, because it look like the content you're displaying may come from users inputs
you need to fight to have your content coming like that :
"{\"name\":\"StatementItem ID\",\"ind ex\":\"Ite mID\",\"wi dth\":24,\ "sortable\ ":false}"
"{\"name\":\"StatementItem
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Alas I think it's the only useful question.
Perhaps you missed something in the previous stage and now you want to do a late correction