English 中文(简体)
If Javascript code block is not at end of HTML file, but is using jQuery s $(document).ready(function() {...}), will it slow down the page display?
原标题:

It is said that Javascript code should be all placed at the end of HTML file, so that the page content is shown first, for the user to see something (so that the user is satisfied to see something instead of waiting another 12 seconds, for example).

But to better encapsulation of the HTML and match Javascript code, such as an "Image Carousel", then usually the HTML and Javascript is placed in one single file, so there are Javascript code blocks all intermixed with HTML code throughout the final HTML file.

But what if all these Javascript code blocks use jQuery s $(document).ready(function() { ... }) to perform the task, then won t the page display be very fast as well? I think not as fast as when the Javascript is actually placed at the end of the HTML file, but close enough, because it merely adds a function to the ready event queue.

最佳回答

I think the point is to place the js at the bottom of the page (usually just inside the closing </body> tag) so that the content of the page is displayed while the js is downloading.

If you have your jQuery code spread out throughout the HTML in separate .ready() calls, then no matter what, it won t run until the <body> has fully loaded. So the question would be how much javascript do you have in the HTML?

If there s quite a bit, then it will slow down the display of any content that comes after each script. If it is a relatively small amount of code, then it won t likely make much noticeable difference.

If it is really important to you to have the page s content displayed as soon as possible, then place all scripts after the content.

I personally wouldn t mix javascript with HTML just for the sake of association. You could have unexpected results if you start removing/appending content that happens to include a script. I d rather use appropriately named classes and IDs, as well as lots of code comments.

Also keep in mind that those .ready() calls won t work until jQuery has loaded, which would mean that it would need to be at the top of the page, or at least before your first call.

So again it gets back to the question of which is more important to you. If you want the content visible as quickly as possible, place all js at the bottom. If you want your method of intermixing js and HTML, you ll have some delay in displaying the page.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签