views:

205

answers:

3

I have a bunch of pages inheriting from a master view. One one of these pages I want to use a popular jQuery plugin called "Uploadify".

Where should I place the reference for the Uploadify javascript file? I only need it on one small page so it didn't seem right to place it in Master view. Is it 'Ok' to place it in the content tag of my inherited view that will use this plugin?

+6  A: 

The best way I know to do this is to add a ContentPlaceHolder in the <head> section of the MasterPage, and add the <script> tags to a Content referencing that section in all pages that need extra javascripts (or stylehseets, or whatever... It certainly adds an extra degree of freedom).

In you master:

<head>
    <!-- Sitewide script references, title tags etc goes here -->

    <asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>

As it is empty by default, you don't have to change anything in any other pages to make this change to your master.

In your page that needs the extra js script:

<asp:Content ID="HeadContentFromPage" ContentPlaceHolderId="HeadContent">
    <script type="text/javascript" src="myPageSpecificScript.js"></script>
</asp:Content>
Tomas Lycken
Agreed, but I'd put it at the bottom just above the end body tag.
Charlino
In case he's using the jQuery $(document).ready(function() { ... }); it doesn't really matter - the code won't run until the DOM has loaded anyway.
Tomas Lycken
A: 

Put a ScriptManager control on your master page and include the commonly used JS files there:

<asp:ScriptManager ID="ScriptManager1" runat="server" >
  <Scripts>
    <asp:ScriptReference Path="Common.js" />
  </Scripts>
</asp:ScriptManager>

Put a ScriptManagerProxy control on your content pages (or on any user controls) and include the specific JS files there:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" >
  <Scripts>
    <asp:ScriptReference Path="Specific.js" />
  </Scripts>
</asp:ScriptManagerProxy>

EDIT: I'm not sure if this also works with ASP.NET MVC.

M4N
It does not, as there is no <form runat="server"> tag in an ASP.Net page (or masterpage).
Tomas Lycken
@Tomas: Thanks for the comment, good to know that!
M4N
A: 

You could add a content block in the portion of your master page. That would keep the reference to your js file in the document head and out of the document body.

Also, if you use static document caching, it probably doesn't matter much whether you put the document in your master page or in the view, especially if most people will eventually upload a document. Although this approach may make the first page load a little bit slower.

Jarrett Meyer