views:

1363

answers:

2

Hi folks,

simple question i'm sure (blush). I have a simple View and I wish to add a JQuery DatePicker javascript to this view (and not every view, via a masterpage).

I'm not sure what is the best way to do this.

Second, I'm conscience of where/when my Javascript loads. I'm a fan of YSlow and it recommends that i add any scripts to the bottom of the page, which I do.

So .. how could i do both?

Here's the view

<%@ Page
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    <% using (Html.BeginForm()) {%>

    <p>
        <label for="StartDate">Start Date:</label>
        <!-- JQuery DatePicker to be added, here. -->
    </p>
    <% } %>
</asp:Content>

cheers ;)

+5  A: 

One way is put a content placeholder at the bottom of your master page, then the specific slave page that needs the javascript can specify the placeholders contents as the required javascript. Other pages that don't need the javascript simply don't specify any content for the placeholder.

Brehtt
Cheers :) For the record, i've added a new ContentPlaceHolder at the bottom of the page, right above the </body> tag. Discussion about that, here: http://developer.yahoo.net/blog/archives/2007/07/high_performanc_5.html
Pure.Krome
Tracker1
+7  A: 

Your MasterPage should have a ContentPlaceHolder for the head.

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

Then you're able to include head elements (JavaScripts, StyleSheets, etc...) in your views:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="/Scripts/jquery/ui/ui.datepicker.js" type="text/javascript">
    </script>
    <link href="/Styles/myStyle.css" rel="stylesheet" type="text/css" />
</asp:Content>
CMS
this answer was better, except that it was putting it in the head .. when i did say it should be added to the bottom of the page. So winob0t gets the tick.
Pure.Krome