tags:

views:

817

answers:

5

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.

+6  A: 

$(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.

Jon Erickson
So, if I am using the Microsoft AJAX library, I do have to call pageLoad, but if I am just using JQuery, I do not, correct?
Xaisoft
you do not HAVE to but you have the option to. personally I use the jQuery way $(function(){}); all the time whether or not I have the MS AJAX libraries loaded.
Jon Erickson
I believe there is a difference between jQuery document ready and the JavaScript PageLoad stuff...
RSolberg
Ok, I was a little confused because in your answer, you said that if you are using the MAJAX libraries, you have to call pageLoad, but it seems like just calling $(document).ready(...) is good enough or am I misunderstanding?
Xaisoft
yea calling the jQuery document ready is good enough.
Jon Erickson
Great! Thanks for the help.
Xaisoft
@Xaisoft - You should validate that there is no difference b/t the PageLoad function and document ready. You can put your jQuery code in either, but you may still need to utilize PageLoad for the MSAJAX stuff...
RSolberg
Ok, so it appears that if I am using MSAJAX, I should use pageLoad.
Xaisoft
@Xaisoft - not what I'm saying... Jon's example for the document ready will work just fine for background color sample, but you may still need PageLoad for MSAjax stuff. It wouldn't hurt to have the PageLoad and the DocumentReady
RSolberg
Ok, I got it. Thanks
Xaisoft
pageLoad and document.ready fire at different times. pageLoad fires AFTER document.ready. I've been burnt by this. I use pageLoad myself because it's more reliable. I have document.ready fire when the document was not actually fully loaded.
Robert C. Barth
pageLoad is probably working around the fact that IE sometimes fires document.ready before the document is actually ready.
Adam Lassek
+3  A: 

Adding your scripts through ScriptManager in that way has the advantage of making it easy to concatenate+minify your files by using CompositeScript. Unfortunately, that means they'll be referenced on the page through ScriptResource.axd, which I have always found to be an extremely ugly solution.

I'm much more interested in integrating something like juicer into my build process, but you can't beat ScriptManager for convenience.

Adam Lassek
If you have a masterpage, do you put the ScriptManager in the masterpage only?
Xaisoft
That's up to you. If you do, you can use the ScriptManagerProxy on the child pages.
Adam Lassek
Ok, so if you put the ScriptManager in the master page, you can't put another ScriptManager on a page that is using the masterpage, you would have to use the ScriptManagerProxy instead. Correct?
Xaisoft
No, the only time you would need a ScriptManagerProxy on a Content Pages is if you need to use an additional script for that page.
Gordon Bell
Yes that is what I meant, but you can't use two ScriptManagers?
Xaisoft
That's correct--you can only have one ScriptManager. If you need to add more scripts on a child that the master page isn't, ScriptManagerProxy will do that.
Adam Lassek
+2  A: 

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>
Gordon Bell
I'm new to this. What do you mean by single composite script?
Xaisoft
http://msdn.microsoft.com/en-us/library/cc488552.aspx
Gordon Bell
ASP.NET combines all the .js files into a single compressed script before sending to the browser. Good if you're using multiple non-minified scripts.
Gordon Bell
+3  A: 

If you are looking at how to reference the jQuery file, this is what I am doing:

<script type="text/javascript" src="Scripts/jquery.js"></script>

Since you will have the ScriptManager on your page, you will also be able to tap into PageLoad.

function pageLoad() {
    //MSAJAX Stuff...  If Needed
}

$(document).ready(function() {
    $.datepicker.setDefaults($.datepicker.regional[$("#<%= hfCultureAbbreviation.ClientID %>").val()]);
});
RSolberg
Thats how I was normally doing it. Why do you need the PageLoad? and I am net to javascript, so what do the Sys.WebForms.PageRequestManager.getInstance() methods do in your example?
Xaisoft
@Xaisoft - it creates 2 javascript functions for me that run after an AJAX request is completed and one before the request is sent back to the server. This helps with validation, etc. with some jQuery UI components and other system needs.
RSolberg
New to javascript, so it is a little over my head, but thanks for the example.
Xaisoft
+2  A: 

Don't forget to register the Intellisense file as well! Sometimes can be pretty handy.

Mark Kadlec
I was curious why I wasn't getting intellisense. Where can I find that file?
Xaisoft
http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2-vsdoc.js
Gordon Bell
Just save the vsdoc file in the same folder as your jquery-1.3.2-min.js or jquery-1.3.2.js
Gordon Bell
If Intellisense doesn't work after doing that, you may need the hotfix:
Gordon Bell
http://weblogs.asp.net/scottgu/archive/2008/02/08/vs-2008-web-development-hot-fix-roll-up-available.aspx
Gordon Bell