Merely out of convenience, I'm hoping to, using a server side language, just output a simple <script>
tag with jquery's document.ready()
function. Is this considered bad practice? I know some people consider inline javascript bad practice, which makes me wonder about this.
The script tag should always be used before the body close or at the bottom in HTML file. The Page will load with HTML and CSS and later JavaScript will load.
Yes, it does affect the performance of the webpage loading. The problem is, normal <script> tags are blocking so everything after the script tag has to wait till the script tag is done loading and parsing before the rest of the page can load.
The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.
The “script” tagJavaScript programs can be inserted almost anywhere into an HTML document using the <script> tag.
It's not a problem from the technical point of view, because document.ready()
will fire only after the DOM is fully parsed.
I wouldn't consider it good style, though. It can make maintenance hellishly difficult, because you can't see what's happening on document load at one glance any more.
I would aim to keep everything in one document.ready()
block if at all possible, and that block in the head
or even an external script file if at all possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With