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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With