最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JSON String as Javascript function argument - Stack Overflow

programmeradmin0浏览0评论

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 "#". Without return 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
 |  Show 4 more ments

3 Answers 3

Reset to default 5

Use &quot; for your double quotes, then in js_func(), replace them with actual double quote characters (") before evaluating your JSON string. (thanks for the demo Matthew, I updated your fiddle with the example from the question:)

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)

发布评论

评论列表(0)

  1. 暂无评论