Subscribedefaults write com.apple.Safari EnableDebugMenu 1Safari's Javascript console, in the debug menu, shows the following error:
Value undefined (result of expression re.compile) is not object. bloggie_05.json line 16.
re.compile(spams[i].regExEscape(),'gim');6. Here's all the code, in context:
<script language="javascript">var spams = new Array();spams[0] = 'casino';spams[1] = 'craps';spams[2] = 'backgammon';spams[3] = 'cash advance';</script>[I abbreviated the blacklist (the spams[] array) because this question is already way too long.]
<script src="bloggie_05.js" language="javascript" type="text/javascript"></script>
<textarea tabindex="1" id="comment-text" name="comment_text_body" style='width:95%;height:300px;' onKeyUp="javascript:doCommentPreview();"></textarea>And here's the empty div underneath it, which is filled by the doCommentPreview() function above:
<div class="ientrycomment_body" id="comment-preview" name="prevu"></div>Finally, here's the code in the external (bloggie_05.js) javascript file:
function doCommentPreview(doc){
doc=(typeof(doc)=="undefined"?document:doc);
field=doc.getElementById("comment-text");
preview_field=doc.getElementById("comment-preview");
preview_field.innerHTML = "<p>" + field.value.split(/\n\n/).join("</p><p>").split(/\n/).join("<br />").showSpams() + "</p>";
}
String.prototype.showSpams = function(){
despammed=this;
re = new RegExp();
for (var i=0; i < spams.length; i++) {
re.compile(spams[i].regExEscape(),'gim');
despammed=despammed.replace(re,"<span style=\"color:red;border:1px dotted;\" title=\"you can't say that here!\">" + spams[i] + "</span>");
}
return despammed;
}
String.prototype.regExEscape = function() {
text=this;
if (!arguments.callee.sRE) {
var specials = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\' ];
arguments.callee.sRE = new RegExp(
'(\\' + specials.join('|\\') + ')', 'g'
);
}
return text.replace(arguments.callee.sRE, '\\$1');
}The line that WebKit has a problem with is in bold, in the String.prototype.showSpams() above.
try {
re.compile(spams[i].regExEscape(),'gim');
}
catch {
alert('guess we found the problem');
// write a longer method of doing the same thing without use of the compile method
}
if (!re.compile) alert('this browser doesn't support compile');
for (var i=0; i < spams.length; i++) {br>
...
>spams array every time you iterate through the loop. You could optimize it by declaring a variable in the for...loop that holds the value of the length as such:
for (i=0, j=spams.length; i < j; i++) {
...
preview_field.innerHTML = "<p>" + field.value.split(/\n\n/).join("</p><p>").split(/\n/).join("<br />").showSpams() + "</p>";
Javascript code (for pages) rarely needs to be optimized.Are you insane? If a page takes .1 seconds to load and render, only the peversely obsessive will optimize associated code to save .0001 seconds and make it more opaque.
This is absolutely not true. Since JavaScript is not compiled, there are dozens and dozens of optimization routines that can speed up script execution. Just for example, take this loop in the code you quote:
for (var i=0; i < spams.length; i++) {br>
...
You're calculating the length of thespamsarray every time you iterate through the loop. You could optimize it by declaring a variable in the for...loop that holds the value of the length as such:
for (i=0, j=spams.length; i < j; i++) {
...
for (x=0; x < 100; x++) {
yo = document.forms[0].elements[1].value * x;
formValue = document.forms[0].elements[1].value;
for (x=0; x < 100; x++) {
yo = formValue * x;
...
for (i=spams.length; i > 0; i--) {
...
You are not logged in, either login or create an account to post comments
Why don't you just pass the pattern to 'new RegExp(pat)'?
posted by MonkeySaltedNuts at 1:46 PM on September 23, 2006