开发者

How can I put JavaScript code at the bottom of an ASP.NET page?

In my ASP.NET page, I am referring to an external JavaScript file.

As per my learning in the web, it's recommended always to put inline JavaScript code at the bottom of the page. There is no information 开发者_如何学JAVAabout how to do it for an external JavaScript reference.

I want to know, if I am referring to an external JavaScript file, where should I write it?

> 1. Inside <Head/> top of the page
> 2. bottom after closing tag of </form>


If you're going to use that script dynamically for each page, there is better way to do it. Put this into your masterPage,

protected void Page_Load(object sender, EventArgs e)
{
    string a = "$(function() {";
    a +="$('.Utility.division() .items').hide();";
    a += "});";
    a += "</script>";
    Page.RegisterClientScriptBlock("a", a);
}

Utility.division() is your dynamic variable which is declared in utility.cs//.


Putting JavaScript code at the bottom of the page is effective only if that's an inline script.

Checkout the Google page-speed tutorial for more information.

Recommendations

Put external scripts after external stylesheets if possible.

Browsers execute stylesheets and scripts in the order in which they appear in the document. If the JavaScript code has no dependencies on the CSS files, you can move the CSS files before the JavaScript files. If the JavaScript code does depend on the CSS contained in an external file — for example, styles that are needed for output you are writing to the document in the JavaScript code — this isn't possible.

Put inline scripts after other resources if possible.

Putting inline scripts after all other resources prevents blocking of other downloads, and it also enables progressive rendering. However, if those "other resources" are external JavaScript files on which the inline scripts depend, this might not be possible. In this case, it's best to move the inline scripts before the CSS files.


@spielersun: I think you mean Utility.division() is your dynamic variable which is declared in utility.js rather than utility.cs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜