1

I'm having some trouble getting JSON to store some Javascript as a string value.

{
"code": "function test(){alert('coreg.js has been loaded'); if (jQuery) { alert('We have jQuery!'); } else { alert('We don\'t have jQuery...'); } }"
}

When I validate this using www.jsonlint.com it says I've got a syntax error (unexpected TINVALID at line 2) but I can't for the life of me see what it is. The Javascript is all entirely valid, all I did was take my function and put double quotes outside it.

I'm really confused! Does anyone know what I'm doing wrong?

1 Answer 1

1

you need to escape the escape: We don\\'t have jQuery

{
"code": "function test(){alert('coreg.js has been loaded'); if (jQuery) { alert('We have jQuery!'); } else { alert('We don\\'t have jQuery...'); } }"
}

though if you are using json to get JS code from another domain, you wont be able to do it unless the other domain supports JSONP, or you are using grease monkey's XHR

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I realised it didn't like the backslash but forgot about double-escaping... stupid mistake :P Thanks! (and yeah, I'm using JSONP)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.