McKay
McKay

Reputation: 12604

What are the best practices to add jquery to asp.net?

I apologize if this question has been asked, but I can't find a question for this anywhere.

I would like to use jquery on my asp.net page, but should I just register a new script? Add a link? Is there some special reference I can add?

scottgu states that it's integrated into visual studio, but doesn't describe how to get started adding it to a page?

Upvotes: 3

Views: 993

Answers (4)

McKay
McKay

Reputation: 12604

I don't claim to be an expert, but according to my recent research, something like this is best?

        this.Page.ClientScript.RegisterClientScriptInclude("jQuery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js");

But the documentation states

"Call the IsClientScriptIncludeRegistered method to determine whether a client script include with a given key and type pair is already registered and avoid unnecessarily attempting to add the script." it does also mention "The benefit of checking is to reduce unnecessary computation." So, maybe something like this is better:

    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered("jQuery"))
    {
        this.Page.ClientScript.RegisterClientScriptInclude("jQuery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js");
    }

That seems dumb though, because if I call

        this.Page.ClientScript.RegisterClientScriptInclude("jQuery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js");
        this.Page.ClientScript.RegisterClientScriptInclude("jQuery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js");

It does the same thing as if I just do it once.

Upvotes: 1

Aristos
Aristos

Reputation: 66641

I can not call them best practice, but some notes:

Add a link is the first think that you can do, if you register it as new script you add extra code to run with out reason.

Where on page: jQuery is a core library so load it on top of the page vs the bottom of the page that we left other javascript function to load and use them after the dom ready.

What version to add and from where ? You can add the minified version of jQuery, and you can ether load it from your site, ether load it from google. This is also depend if you keep only the jQuery or add it together with other script to a common file.

What to look for: In case that you have any conflict with other libraries you can use the jQuery noConflict http://api.jquery.com/jQuery.noConflict/

Optimize all your script: If you have more than one script to load, one optimization is to place them all together to a singe file, minified, gzip it, and add it as link to your pages. This is what I do after I follow and all the previous steps.

Upvotes: 3

Adrian Iftode
Adrian Iftode

Reputation: 15663

I believe the best way is to reference the jQuery files from CDNs.

The difference from manually adding a script tag and Registering in code behind states in your requirements. If you build some custom control which uses jQuery, better to ensure that the scripts references will appear in the page so you'll need to Register from code behind.

If there is a Master page and some javascript which uses jQuery all over the pages, then add it in that Master page.

If the script is used in only a small number of pages, then there is no need to load the scripts in the other 100 pages, so you'll add the tag only those two or three pages.

Upvotes: 1

Mark Redman
Mark Redman

Reputation: 24515

You can add jQuery to asp.net by linking the scripts, just link these like you do your other scripts and stylesheets. [unless I missed something]

Upvotes: 1

Related Questions