Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to reference jquery in asp.net?

Tags:

jquery

asp.net

I watched a pdc session by Stephen Walther and in the presentation he was referencing the JQuery js file like the following:

<asp:ScriptManager id="sm1" runat="server">
<Scripts>
    <asp:ScriptReference Path="~/Scripts/JQuery.js" />
</Scripts>
</asp:ScriptManager>

Is there a advantage or disadvantage to doing it the above way instead of just using a link in the head section of the page.

He was also putting the following into the javascript section of his sample pages to run JQuery:

<script type="text/javascript">

function pageLoad()
{
   $(":text").css("background-color","yellow");
}
</script>

Is the pageLoad necesary above? He mentioned that it is from the Microsoft AJAX library and it waits for the DOM to be finished loading, but I thought the $ symbol in JQuery is just a shorthand for waiting for the DOM to be finished loading.

like image 865
Xaisoft Avatar asked Mar 18 '09 17:03

Xaisoft


People also ask

Can we use jQuery in ASP NET?

JQuery is a JavaScript library. It is helpful and make easy to handle HTML DOM (Document Object Model), Events and Animation and Ajax functionalities. JQuery reduce code compared to JavaScript. Mostly we use JQuery or JavaScript for client side activities and make Ajax call to ASP.NET Web form/mvc, Web service and WCF.

What is $() in jQuery library?

It is common to see jQuery programs call $(document) or $(this) , for example. jQuery objects can represent more than one element in a document, and you can also pass an array of elements to $() . In this case, the returned jQuery object represents the set of elements in your array.


2 Answers

$(document).ready() and pageLoad() are not the same!

http://encosia.com/2009/03/25/document-ready-and-pageload-are-not-the-same/

From the article:

pageLoad() is called just after the DOM has finished loading. this isn’t the only point at which pageLoad() is called though: It is also called after every partial postback.

In the case of initialization code that should run once, $(document).ready() is the ideal solution.

like image 169
Jon Erickson Avatar answered Oct 05 '22 08:10

Jon Erickson


Using the ScriptManager, ASP.NET can create a single Composite Script to reduce the number of browser requests, and also if the browser supports it, compress the script.

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <CompositeScript>
        <Scripts>
            <asp:ScriptReference Path="~/Scripts/Script1.js" />
            <asp:ScriptReference Path="~/Scripts/Script2.js" />
            <asp:ScriptReference Path="~/Scripts/Script3.js" />
        </Scripts>
    </CompositeScript>
</asp:ScriptManager>
like image 24
Gordon Bell Avatar answered Oct 05 '22 10:10

Gordon Bell