Skip to content

24 ways to impress your friends

Vote down?

Mark Wales

Firstly, you should never use document.write() – it has the potential to delete the entire DOM if used in the wrong place. That’s not something you want to do! There are always better alternatives, like using innerHTML (supported in all browsers, and much safer).

Maybe I’m missing something, but this code seems unnecessarily complicated – and relies on jQuery, which you shouldn’t force developers to put on their site (don’t get me wrong, I’m a big fan of jQuery, but if you’re only using it for a few lines of DOM manipulation then 31KB is a big price to pay).

Could you not do something as simple as:

HTML
<div id=“pun-of-the-day”></div>

<!— bottom of page —>
<script src=“http://adnetworksite.ads/puns.js”></script>

AD JAVASCRIPT

// Self-executing function (to keep variables out of global scope

(function () {

var div = document.getElementById(‘pun-of-the-day’); div.innerHTML = ‘A good pun is its own reword’;

}());

======

Obviously the ad javascript would be more clever than that and have multiple puns in and such. But it works. And it involves less code for both ends.

As long as the script is called at the bottom of the page (which it should be anyway) this will work. No jQuery needed, no complicated DOM manipulations.