JavaScript in Master Pages

Last post 09-03-2009 9:40 AM by suthish nair. 8 replies.

Sort Posts:

  • JavaScript in Master Pages

    03-22-2006, 10:49 AM
    • Participant
      1,728 point Participant
    • joee
    • Member since 11-13-2002, 2:11 PM
    • Posts 374

    Is it possible to create an area in the master page where I can add javascript to the content pages? I tried adding an asp:contentHolder tag inbetween the opening and closing <head> tags, but that doesn't seem to work. I need to be able to add custom javascript to some of my content pages. Finally, is there a way to get the maseter.cs code-behind to populate to content.cs files? For example, I put a method in my master.cs file but it does not show up in code-behind files of the content pages.

     

    Thanks

  • Re: JavaScript in Master Pages

    03-22-2006, 1:27 PM
    • Star
      8,834 point Star
    • MorningZ
    • Member since 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,815

    joee wrote:
    I tried adding an asp:contentHolder tag inbetween the opening and closing <head> tags, but that doesn't seem to work

    An <asp:ContentPlaceHolder> is for, well, "content"

    There's at least two ways to include the js file from the content page

    1) Use the ClientScript.RegisterScriptBlock class to do it, one "problem" with this is that it'll add the <script> tag somewhere inside the <body> tags.... i always read that inside the <head> tag is the best place for the script tags, so you could alternatively do:

    2) Create a new "HtmlGenericControl" with TagName of "script" and add the necessary attributes of "src" and "type",

    Dim si As New HtmlGenericControl
    si.TagNane = "script"
    si.Attributes.Add("type", "javascript")
    si.Attributes.Add("src", "/includes/somescript.js")


    and then using the fact that your aspx page using your master page has a built in "hook" to the <head> tag in the master page, and its simply accessed it by saying: "Header", you'd say:

    Header.Controls.Add(si)

    and now your <script> reference should be inside the <head> tags

    "If you make it idiot proof, they'll build a better idiot"
  • Re: JavaScript in Master Pages

    04-05-2006, 3:18 PM
    • Participant
      904 point Participant
    • joelny
    • Member since 03-12-2004, 2:12 PM
    • Posts 191

    I know this is a very old thread but I desperate need help on this...instead of adding a <script> tag, I need to inject a <meta> tag into each page from a PageBase file.

    What I have now:

    private

    HtmlGenericControl _headmeta;

    _headmeta =

    new HtmlGenericControl();
    _headmeta.TagName = "META";
    _headmeta.Attributes.Add("HTTP-EQUIV", "Pragma");
    _headmeta.Attributes.Add("CONTENT", "no-cache");
    Header.Controls.Add(_headmeta);

    _headmeta =

    new HtmlGenericControl();
    _headmeta.TagName = "META";
    _headmeta.Attributes.Add("HTTP-EQUIV", "Expires");
    _headmeta.Attributes.Add("CONTENT", "-1");
    Header.Controls.Add(_headmeta);

    This does't seem to work well for me...when I check my HTML source I see the meta tags added outside of <head></head>...please help!

    Thanks,
    JL
  • Re: JavaScript in Master Pages

    04-06-2006, 8:39 AM
    • Star
      8,834 point Star
    • MorningZ
    • Member since 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,815

    HtmlGenericControl is *not* what you want

    There are "HtmlMeta" controls...

    See the very last post here:
    http://forums.asp.net/2/948765/ShowThread.aspx

    Exept keep in mind that it's even easier than that, you can simply say "Header", which is a built in "hook" to the MasterPage's header, to reference the <head> tag

    "If you make it idiot proof, they'll build a better idiot"
  • Re: JavaScript in Master Pages

    04-12-2006, 11:20 AM
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 3:18 PM
    • Citizen of the Earth
    • Posts 1,228
    • TrustedFriends-MVPs

    One of the easiest approaches I've found is to use a ContentPlaceHolder control inside the <head> tag (see The ContentPlaceHolder - Not Just For Content).

    I have a few other approaches to try in this article: Master Pages: Tips, Tricks, and Traps.

    Scott
    http://www.OdeToCode.com/blogs/scott/
  • Re: JavaScript in Master Pages

    02-20-2008, 4:55 PM
    • Member
      6 point Member
    • jaywon
    • Member since 02-10-2008, 10:22 AM
    • Honolulu
    • Posts 2

    For MorningZ post, this works well, although as noted there are probably other/better ways? For this to work correctly though, couple fixes for anyone attempting to cut and paste. The TagName is obvious, but if you don't specify "text/javascript", the browser will download the file but not load it as a js file.

    Slightly polished, but necessary if someone is trying cut and paste Big Smile

     

    Dim si As New HtmlGenericControl
    si.TagName = "script"
    si.Attributes.Add("type", "text/javascript")
    si.Attributes.Add("src", "/includes/somescript.js")

  • Re: JavaScript in Master Pages

    02-25-2009, 10:25 AM
    • Member
      8 point Member
    • harrison.chow
    • Member since 02-18-2009, 1:12 PM
    • Plano, TX
    • Posts 24

     where should i put this code in??  

    Dim si As New HtmlGenericControl
    si.TagName = "script"
    si.Attributes.Add("type", "text/javascript")
    si.Attributes.Add("src", "/includes/somescript.js") 
     thanks!
    Harrison Chow
    www.aghealth.com
  • Re: JavaScript in Master Pages

    02-25-2009, 3:54 PM
    • Member
      6 point Member
    • jaywon
    • Member since 02-10-2008, 10:22 AM
    • Honolulu
    • Posts 2

     Best place for it is probably in the Page_Init or Page_Load functions in the code-behind. Personally, I would put it in Page_Init, although either will suffice.

     Also, don't forget to "attach" the HTMLGenericControl to the page:

     Header.Controls.Add(si)

  • Re: JavaScript in Master Pages

    09-03-2009, 9:40 AM

     

    refer about Master/Content Pages : http://odetocode.com/Articles/450.aspx 


    Mark as Answer
    on the posts replys that helped you.
    My Blog -: MSChart

Page 1 of 1 (9 items)