I saw some jquery code on the Net somewhere, that took this form :
<script>
function doSomething(message)
{
$(document).ready(function(){
alert(message);
});
};
</script>
i.e. an external function (“doSomething”) which has $(document).ready inside it. I’m confused because isn’t the code under $(document).ready triggered when the DOM is loaded ? Its like having an event handler inside a function (?). Does this form of code make sense to anyone ? Thanks.
It was probably an error on the part of the original coder. You could do that, but you’d want to call
doSomethingpretty early on. Note that if the DOM is already ready when you callready, jQuery will call the code, so it will happen either way, but it won’t happen until someone, somewhere, callsdoSomething. I can’t see much purpose to the pattern, so unless you have some strong argument for using it — and given the question, I suspect you don’t 🙂 — you can safely ignore it.