Created
May 17, 2011 15:29
-
-
Save katspaugh/976695 to your computer and use it in GitHub Desktop.
createElement proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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