The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex
JSON.org has JSON parsers for many languages including 4 different ones for Javascript. I believe most people would consider json2.js their goto implementation.
ReplyDeleteDon't bother with that crap. If you're using jQuery just use:
ReplyDeletejQuery.parseJSON( jsonString );
It's exactly what you're looking for
http://api.jquery.com/jQuery.parseJSON/
Why not just:
ReplyDeleteJSON.parse(jsonString);
I'm not sure about other ways to do it but here's how you do it in Prototype (JSON tutorial).
ReplyDeletenew Ajax.Request('/some_url', {
method:'get',
requestHeaders: {Accept: 'application/json'},
onSuccess: function(transport){
var json = transport.responseText.evalJSON(true);
}
});
Calling evalJSON() with true as the argument sanitizes the incoming string.
If you're using jQuery, you can also just do $.getJSON(url, function(data) { });
ReplyDeleteThen you can do things like data.key1.something, data.key1.something_else, etc.
$.ajax({
ReplyDeleteurl: url,
dataType: 'json',
data: data,
success: callback
});
The callback is passed the returned data, which will be a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method.
JS Guru Douglas Crockford has written a parseJSON function which you download here
ReplyDeleteI have successfully been using json_sans_eval for a while now. According to its author, it is more secure than json2.js.
ReplyDelete