Skip to content

Instantly share code, notes, and snippets.

@katspaugh
Created May 17, 2011 15:29
Show Gist options
  • Save katspaugh/976695 to your computer and use it in GitHub Desktop.
Save katspaugh/976695 to your computer and use it in GitHub Desktop.
createElement proxy
(function (docInstance) {
var Monitor = function () {
return this.init.apply(this, arguments);
};
Monitor.prototype = {
Resources: {
img: true,
script: true
},
init: function (tagName) {
var el = this.create(tagName);
if (this.Resources[tagName.toLowerCase()]) {
el.__defineSetter__(
'src', this.setSource
);
}
return el;
},
create: (function () {
var doc = docInstance.constructor.prototype,
create = doc.createElement;
doc.createElement = function (tagName) {
return new Monitor(tagName);
};
return (function (tagName) {
return create.call(
docInstance, tagName
);
});
})(),
/* NB: the setter is called in the context of a DOM-element. */
setSource: function (src) {
console.warn('Setting the source property of %o to %s.', this, src);
delete this['src'];
this.src = src;
this.__defineSetter__(
'src', Monitor.prototype.setSource
);
}
};
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment