javascript - Does Crockford's "deentityify" method in The Good Parts actually work? -
this particular code has been asked twice before, never question raised "does anything?". appears in page 41 of crockford's book.
here syntactic sugar earlier in book:
object.prototype = function (name, func){ if(!this.prototype[name]){ this.prototype[name] = func; }
and here deentityify methody:
string.method('deentityify', function ( ) { var entity = { quot: '"', lt: '<', gt: '>' }; return function () { return this.replace( /&([^&;]+);/g, function (a, b) { var r = entity[b]; return typeof r === 'string' ? r : a; } ); }; }());
if in editor write like:
document.writeln( '<">'.deentityify( ));
you'll see in browser: however, if type in browser: document.writeln( '<">');
you'll still see:
<">
i discovered method isn't replacing entities when attempted use when pre-filling form data setting value field this:
var venueinput = document.createelement("input"); ... venuename = '<>"'; // example venueinput.value = venuename.deentityify(); form.appendchild(venuename); form.appendchild(venueinput);
alert show entities aren't being replaced.
can me see i'm doing wrong? thank you!
after fixing mistakes of code shared works charm:
object.prototype.method = function (name, func){ if(!this.prototype[name]){ this.prototype[name] = func; } } string.method('deentityify', function ( ) { var entity = { quot: '"', lt: '<', gt: '>' }; return function() { return this.replace( /&([^&;]+);/g, function (a, b) { var r = entity[b]; return typeof r === 'string' ? r : a; } ); }; }()); var venueinput = document.createelement("input"); var label = document.createelement("label"); venuename = '<>"'; // example venueinput.value = venuename.deentityify(); label.innerhtml = venuename; document.getelementbyid('form').appendchild(label); document.getelementbyid('form').appendchild(venueinput);
<div id="form"></div>
Comments
Post a Comment