Caveatrob
Caveatrob

Reputation: 13267

master and content pages and jquery

I want to use jQuery in my ASP.NET 3.5 website that uses master pages and content pages. How do I do document ready() functions for the child pages for those that will use jQuery ? Where do I put the code?

I figured the jQuery declarations should go in the master page, but don't know how to make sure any jQuery calls go into the HEAD of the resolved page.

Upvotes: 4

Views: 9728

Answers (3)

Mike Marshall
Mike Marshall

Reputation: 7850

Just put a content placeholder in the master's head section, and then write page-specific scripts withing the asp:Content tags of your pages.

Master

<%@ Master Language="C#" %>
<html>
    <head>
        <script type="text/javascript" src="scripts/jquery-1.4.3.js"></script>
        <asp:contentplaceholder id="LocalScripts" runat="server" />
    </head>
    <body>
    </body>
</html>

Page

<%@ Page Language="C#" MasterPageFile="~/Master1.master" 
         AutoEventWireup="true" Title="MyTitle"  %>
<asp:Content ID="Content1" Runat="Server" ContentPlaceHolderID="LocalScripts" >
    <script type="text/javascript"> 
        $(document).ready(function () {
           // some local script logic here
        });
    </script>
</asp:Content>

Upvotes: 6

Rion Williams
Rion Williams

Reputation: 76547

You should be able to just use something like this:

In child page (I don't think it matters greatly WHERE it is put in the page):

<script type="text/javascript">
    $(document).ready(function () {
        //Do work here
    });
</script>

as long as you have included the necessary jQuery files (libraries and plug-ins) in your master page.

Upvotes: 2

Spencer Ruport
Spencer Ruport

Reputation: 35117

$(document).ready() calls can be made anywhere on the page, not just in the head. If you do want your child pages to place additional scripts in the head area I'd suggest simply defining another ContentArea in your master page inside the head tags.

Upvotes: 1

Related Questions