$(function(){}) is the the same as jquery’s $(document).ready so I am wondering if I can put it into body
instead of head ?
I only see the act of putting script at different section of the page as a matter of executing it at different time. It is true? Can I put script in a div in the middle of the page? Will it affect how DOM is loaded?
$(function(){}) is the the same as jquery’s $(document).ready so I am wondering if I
Share
Yes, but the script you’re talking about just makes a single function call (to
ready). The callback will get called later, when the DOM is ready, either way.Yes. But again, if the script in question is just calling
ready(directly, or via the shortcut), it doesn’t much matter. I would discourage you from littering scripts all over your markup; best to try to keep the two largely separate.Only if you use
document.writewithin the script (and even then, it doesn’t affect how the DOM is loaded, but may affect the content of it).If you can choose where the
scripttags go (e.g., you’re not writing a JavaScript library or jQuery plug-in, your script is on a page you control), there’s little if any reason to useready. Instead, put your script tag at the end of the page, just before the closing</body>tag. References: