2

I'm creating a bookmarklet that will accept a JSON string through a prompt like so:

prompt("Enter your JSON", "");

Once they enter their JSON string, I convert their string into a JSON Object using JSON.parse. Unfortunately, as far as I can tell JSON.parse is not supported in IE9. The object of my bookmarklet is to loop through the available <input>'s on the page and fill their values with data from my JSON object.

What can I do in order to get this to work in IE9?

One possible solution - This answer: https://stackoverflow.com/a/7146404/556079 suggests that JSON.parse will work if my document is in standards mode. Since this is a bookmarklet, I have no control over the doctype of the page. Would it be possible to change the doctype using my bookmarklet?

I would prefer to avoid loading libraries in my bookmarklet like http://bestiejs.github.io/json3. If this can be done without resorting to that, that would be ideal.

Alternatively, the input doesn't need to even be a JSON. I just need to get some user submitted data into a loop where I can reference anywhere between 2 and 50 values.

Community
  • 1
  • 1
jkupczak
  • 2,891
  • 8
  • 33
  • 55
  • Try having your bookmarklet load a JSON library like http://bestiejs.github.io/json3/ – gen_Eric Aug 11 '14 at 17:44
  • P.S. With standards mode, `JSON.parse` works in IE8+ http://caniuse.com/#feat=json – gen_Eric Aug 11 '14 at 17:45
  • I'm confused as to why my question is being voted to be closed. The suggested duplicate question does not answer my question. – jkupczak Aug 11 '14 at 18:47
  • I re-opened it. It was probably closed since that is a possible solution, though not the one you are looking for. – gen_Eric Aug 11 '14 at 18:49
  • "The input doesn't need to even be a JSON. I just need to get some user submitted data into a loop where I can reference anywhere between 2 and 50 values." In that case, make your own format, with its own parser (regex or whatever). Let users know how to enter in the data. – gen_Eric Aug 11 '14 at 18:50
  • 4
    If it's a bookmarklet - I'd probably just do `eval('(' + strData + ')')` *hides* – Benjamin Gruenbaum Aug 11 '14 at 18:50
  • @BenjaminGruenbaum: You know, I didn't even think of that. `eval` just never crosses my mind. Though here, it might be a fine solution. – gen_Eric Aug 11 '14 at 18:51
  • Right now my users would submit a JSON string like this `[{"name":"Example","url":"http://example.com"}]`. Then I would loop through it to get the values. If `eval()` is a suitable replacement for `JSON.parse` I'll look into it as I've never used `eval()` before. – jkupczak Aug 11 '14 at 18:59
  • 1
    @jimmykup: Before `JSON.parse` was widely supported, `eval('(' + strData + ')')` was used. – gen_Eric Aug 11 '14 at 19:04

0 Answers0