Adding a script file to a contentplaceholder page

Alanf718
Alanf718 used Ask the Experts™
on
Hello Iam decided to switch over to using master pages in a web application ive been building because its getting more and more addon with each passing day.  One issue i have about using master pages/ content pages is that i would normally have dedicated javasript (.js) files referenced for each page to carry out page specific functionality, but when using content pages i dont know of how (if possible) to add the script file to the <head> element since it lives in the master page.  Anythoughts on how i can do that.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
From the information from : http://www.dotnetcurry.com/ShowArticle.aspx?ID=274&AspxAutoDetectCookieSupport=1

I know there is a script option you can use in the content page below the page, OutputCache, MasterType, etc.,  directives; Although I have not used this.

<%@ Page AutoEventWireup="false" Inherits="_Default" Language="VB" MasterPageFile="~/MyMaster.master" Title="Main" %>
  <script src="Scripts/TestScript.js" type="text/javascript"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
</asp:Content>
Apologies,  I have come across a page of mine and found a working example of a script on a content page.  The script is inside of the content.  Try your scipt inside as in the snippet below ...


<%@ Page Title="BAR = Test 2" Language="VB" MasterPageFile="~/DCNR.master" AutoEventWireup="false" CodeFile="test2.aspx.vb"
  Inherits="test2" Theme="DCNR" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ MasterType TypeName="DCNR" %>
<asp:Content ID="Content1" runat="Server" ContentPlaceHolderID="ContentPlaceHolder1">
  <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
  </cc1:ToolkitScriptManager>

  <script language="javascript" type="text/javascript">
    // Boat Drags
    function mousedowncb(event) {
      event = event || window.event;
      document.oldClientX = event.clientX;
      document.oldClientY = event.clientY;
      var sender = $get("mydiv");
      var senderLocation = Sys.UI.DomElement.getLocation(sender);
      document.offClientX = event.clientX - senderLocation.x + 1;
      document.offClientY = event.clientY - senderLocation.y + 2;
      document.onmousemove = mousemovecb;
      document.onmouseup = mouseupcb;
      return false;
    }
          <asp:GridView ID="gvImage" runat="server" AutoGenerateColumns="False" DataKeyNames="diagramID" ShowHeader="False">
            <Columns>
              <asp:ImageField AlternateText="Working" DataImageUrlField="boatAccidentID">
              </asp:ImageField>
            </Columns>
          </asp:GridView>
 ...

</asp:Content>

Open in new window

Author

Commented:
but I want to load a js file as the script since i dont necessarily want my javascript cluttering up my web page with information that does not necessarily need to live there, and the thing is that the javascript files i write are usually a little lengthy so i would really prefer to not directly put the code on the page
I moved the functions into a separate file, MyScripts.js, and replaced it with the following line within the content control with no problems.


  <script language="JavaScript" src="MyScripts.js"></script>
<script language="javascript" type="text/javascript" src="MyScripts.js"></script>
also worked.

Author

Commented:
Cool worked great thanks for the help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial