admin管理员组文章数量:1333178
I am trying to define a pure JSON string as an argument in a Javascript function.
Below is the way I want to define it:
<a href="#" onclick="js_func('arg_1', 'arg_2', '{"key": "value"}'); return false">Link</a>
Firebug gives me an error alert: unterminated string literal, even when I escape the double-quotes on the JSON string.
How can I solve this?
Thanks.
I am trying to define a pure JSON string as an argument in a Javascript function.
Below is the way I want to define it:
<a href="#" onclick="js_func('arg_1', 'arg_2', '{"key": "value"}'); return false">Link</a>
Firebug gives me an error alert: unterminated string literal, even when I escape the double-quotes on the JSON string.
How can I solve this?
Thanks.
Share Improve this question asked Jan 9, 2011 at 7:06 ObiHillObiHill 11.9k24 gold badges91 silver badges142 bronze badges 9-
whats with the
return false
. can you put more than one mand into an event like that? and no semicolon? im not 100% here so sorry if this is acceptable – jon_darkstar Commented Jan 9, 2011 at 7:14 - @jon, see [HTML: What's the effect of adding 'return false' to an onclick event ? ](stackoverflow./questions/128923/…). – Matthew Flaschen Commented Jan 9, 2011 at 7:16
- @Matt - e on sending me that link is kind of condescending. I know returning fales stops the behavior that would otherwise occur. what is the point here? Why not skip the hyperlink reference at all? – jon_darkstar Commented Jan 9, 2011 at 7:24
- is it not allowed to have an anchor with no href attribute? is that the point? – jon_darkstar Commented Jan 9, 2011 at 7:26
-
@jon, if it seemed condescending, I'm sorry. It wasn't meant to be. You can have an empty
href
, or you can use "#". Withoutreturn false
(or equivalent), the first will cause a reload, and the second will cause a jump to the top of the page. – Matthew Flaschen Commented Jan 9, 2011 at 7:33
3 Answers
Reset to default 5Use "
for your double quotes, then in (thanks for the demo Matthew, I updated your fiddle with the example from the question:)js_func()
, replace them with actual double quote characters ("
) before evaluating your JSON string.
http://jsfiddle/brillyfresh/kdwRy/1/
simply defining the link as <a href="#" onclick="js_func('arg_1', 'arg_2', {key: 'value1'}); return false;">Link</a>
works fine. JSON is valid JavaScript, you don't need to enclose it in ''s.
I also suggest to use an EventListener (element.addEventListener()
), this makes the html cleaner and would reduce this problem to nothing.
ryou are either trying to pass the parsed object or pass a string
Object: onclick="js_func(arg_1, arg_2, {'key': 'value'});"
String: on_click="js_func('arg_1', 'arg_2', '{\"key\": \"value\"}'); return false"
All I've got handy to test is firebug interpreter but both worked fine for me.
>>>>'{\"key\": \"value\"}'
"{"key": "value"}"
>>>> {'key': 'value'}
Object {key="value"}
(I don't mean to presume whether arg_1
and arg_2
are strings or variable names, and it doesnt matter. just did the same thing as with the JSON)
本文标签: JSON String as Javascript function argumentStack Overflow
版权声明:本文标题:JSON String as Javascript function argument - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742348376a2457986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论