Matt Snider JavaScript Resource

Understanding JavaScript and Frameworks

Wednesday, August 8, 2007

Safari 2.0 Regex Bug Crashes Browser

Several of my projects have reported that my JSON objects were not working properly in Safari 2.0 and less. Namely, the browser completely crashes when trying to parse the server returned JSON object. I always use Douglas Crockfords ‘Json.js‘ file to sanitize my JSON messages and could not figure out what was wrong. After some digging and some debugging on a friend’s mac, I learned that Safari does not support regular expression evaluations on data larger than ~5k. The only solution is to not sanitize the JSON in Safari browsers.

Tobie Langel also wrote a blog post about this entitled “Yet Another Safari Bug“. He is focusing on how to leverage the Prototype Framework to do this and has modified the regex to allow slightly larger data set.

I choose to do the following browser detection, because I could not find/write a non-browser detection solution that targeted only Safari and stop the browser from crashing.

if (Core.Client.browser == “Safari”) { return (jsonResponse) ? eval(jsonResponse) : []; } else { return (jsonResponse) ? jsonResponse.parseJSON() : []; }

This leverages the browser detection functions in Core.js.

Update 10/29/2007: On Saturday (10/27/2007) I spoke with Douglas Crockford at the Code Camp conference and he has found the limit to exist in Strings 5000 to 7000 characters long, depending on your machine. Unfortunately, he doesn’t plan to update JSON.js, anytime in the near future, to handle Safari 2, as the browser is being phased out.

Also, Steve @ blog.stevenlevithan.com has written a better Regex Object XRegExp 0.2. I have not spent much time working with it, but it is supposed to defeat the Safari Bug and generally improve JavaScript Regex support.

posted by Matt Snider at 2:05 pm  

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress