18 June, 2008

HowTo override and substitute document.write function in JavaScript (sample)

It is pretty simple to wrap document.write function in JavaScript.

For example - using closures:
<script id="source" type="text/javascript">
function wrapString(str) {
return "(" + str + ")";
}

document.write = function(w) {
return function(s) {
w.call(this, wrapString(s));
}
}(document.write);

document.write("Hello");
document.write("world");
</script>

This "advanced piece of engineering" prints string: (Hello)(world)

How do you like it?

No comments: