Master and Content Pages and jquery

I want to use jQuery on my ASP.NET 3.5 website, which uses master pages and content pages. How to execute ready () functions for child pages for those that will use jQuery? Where can I put the code?

I realized that jQuery ads should go on the main page, but I don’t know how to make sure that any jQuery calls go to the HEAD of the allowed page.

+4
source share
3 answers

Just place the content placeholder in the section of the main chapter, and then write the scripts related to the site with 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> 
+6
source

You should just use something like this:

In the child page (I don’t think it matters a lot, WHERE it is placed on the page):

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

while you have included the necessary jQuery files (libraries and plugins) on the main page.

+2
source

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

+1
source

Source: https://habr.com/ru/post/1337213/


All Articles