admin管理员组文章数量:1426907
I have code in javascript:
var location = '"HCM - NYC (New York, NY)"';
td_Details.innerText = location;
Now I want to decode the text location to
"HCM - NYC (New York, NY)"
Please advice. Thanks.
I have code in javascript:
var location = '"HCM - NYC (New York, NY)"';
td_Details.innerText = location;
Now I want to decode the text location to
"HCM - NYC (New York, NY)"
Please advice. Thanks.
Share Improve this question edited Jan 2, 2013 at 11:23 Ingo Karkat 173k16 gold badges262 silver badges339 bronze badges asked May 3, 2010 at 9:45 jeffjeff 4461 gold badge15 silver badges35 bronze badges 3-
1
You don't want to call your variable
location
, that's a reserved word. It didn't worked too well for me. – Kobi Commented May 3, 2010 at 9:50 -
1
@Kobi it's not a reserved word, it's just a global variable that browsers attach to the
window
object. – Matt Commented May 3, 2010 at 9:53 -
1
@Kobi
location
is not a reserved word. It is a client side object, so using it may be inadvisable but has no negative effects (unless you want to use the object). – Tomalak Commented May 3, 2010 at 10:00
2 Answers
Reset to default 3There is no specific function in JavaScript which will decode HTML entities, however you can assign an innerHTML
property to an element and then read it back.
x = document.createElement('div');
x.innerHTML = ""test"";
console.log(x.innerHTML); // => "test"
This will work for any HTML entities, not just "
edit:
As pointed out below, you're half-way there, you're just using the wrong property.
Change:
td_Details.innerText = location;
to:
td_Details.innerHTML = location;
For future reference, innerHTML
is available in all browsers. innerText
is not.
To remove the " just use the following:
location = location.replace(/"/g, '');
You may have actually meant to include the quotes in your output. To do so, do this instead:
location = location.replace(/"/g, '"');
本文标签: how to decode a string in a variable in javascriptStack Overflow
版权声明:本文标题:how to decode a string in a variable in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744679534a2619317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论