In some browsers, such as Safari 3 and Chrome, passing a long, unspaced string into the 'alert' function will print only 1 line of text, cutting off any part of the string exceeding the alert box width. Most of the time the 'alert' function is used for debugging purposes, and this textual clipping can be make debugging difficult. Here is a simple RegExp that will auto-insert a newline character every 55 characters in a string, before passing the string to the 'alert' method.
Example 1: SpacedAlert Function
spacedAlert: function(s) {
var str = '' + s;
if (55 < str.length) {
str = str.replace(/(\S{55})/g, '$1\n');
}
alert(str);
}
In my personal experience, this has been most useful when checking the serialization of a form before passing it to an AJAX request. Say we have the following serialized form string:
Example 2: A Serialized Form
username=matt+snider&password=asdf1234&email=[email protected]&zipcode=94041&city=mountain+view&browser=firefox
When passed to 'alert' in Safari and Chrome, it will be truncated to:
Example 3: How Safari & Chrome Truncate
username=matt+snider&password=asdf1234&email=mattsnider@
But the 'spacedAlert' function will produce the following alert message:
Example 4: Spaced Alert For A Serialized Form
username=matt+snider&password=asdf1234&email=mattsnider @mattsnider.com&zipcode=94041&city=mountain+view&browse r=firefox
Now the developer can easily read the whole string.
