Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

I have a application which is divided in to two asp:content and i need to add one jquery script reference to that application, the problem is we need to add the script reference in the head section but in this context where should i add the reference?
can anyone suggest?
Posted

It's always a good idea to add your page references in a central place, the Master Page in this case, in the header section where you can easily locate them.

e.g.
HTML
<script type="text/javascript" src="/scripts/js-file.js"></script>
 
Share this answer
 
Ideally you should add reference of of your JavaScript/JQuery file in Header section of Master Page. But you can also add reference in content page as below.
ASP.NET
<asp:content id="Content1" contentplaceholderid="Test" runat="server" >
    <script type="text/javascript" src="Scripts/MyJavaScript.js"></script>
 
Share this answer
 
v2
Comments
gokulnath1 15-Nov-11 5:49am    
but it wont work it will take the script tag as a text and will print the entire line as title.
RaisKazi 15-Nov-11 8:35am    
It should work, Have you tried that? I use it many times, when I want to include my script reference in particular content page.
gokulnath1 15-Nov-11 9:06am    
ya i have tried that and that prints the entire tag as a text for the title.
Hi,

I would suggest that you add the script-file using the page's ClientScript Manager:

C#
ClientScript.RegisterClientScriptInclude(typeof(TestPage), "jQuery-1.4.3", "/scripts/jquery-1.4.3.js");


This will place the script at the top of your content (after the Postback-Scripts from the ASP.NET). Also if you have many controls which requires the same script, you can register the script on each control with the same key and the ScriptManager will load it only once. So you don't have to mess around if a script is already loaded :)

If you insist of loading the script in the header, you could do it this way:

C#
Header.Controls.Add(new LiteralControl("<script src=\"/scripts/jquery-1.4.3.js\" type=\"text/javascript\"></script>"));


This requires that your <head> is runat="server", otherwise this would not work.

Hope this helps.

Best regards and happy coding,
Chris
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900