Masterpages & JavaScript

Last post 11-04-2008 4:54 AM by warnajith. 4 replies.

Sort Posts:

  • Masterpages & JavaScript

    10-31-2008, 9:16 AM
    • Member
      59 point Member
    • DJOuk
    • Member since 01-06-2008, 6:26 PM
    • Posts 160

    If you have a page which is implemented from a MasterPage and you wish to put JavaScript code in the head of that page alone, How would you go about it?

    You place a  ContentPlaceHolder in the MasterPage as that does not seem to be working for me...

  • Re: Masterpages & JavaScript

    10-31-2008, 10:14 AM

    The Content page does not have a <head> element. However when the page is rendered, the contents of the masterpage and the contentpage are combined, so you get the <head> as defined in the masterpage.

    Anyways, going back to your problem. You can use javascript anywhere on your ContentPage and call it.

     

  • Re: Masterpages & JavaScript

    11-03-2008, 6:57 AM
    • Member
      59 point Member
    • DJOuk
    • Member since 01-06-2008, 6:26 PM
    • Posts 160

     Seems kind of messy, Ideally I wanted to put my functions in the head. I may just put them in a seperate file and then import the file from the Masterpage. But if I use the masterpage across the site it's not ideal.

  • Re: Masterpages & JavaScript

    11-04-2008, 2:53 AM
    Answer

    DJOuk:

    If you have a page which is implemented from a MasterPage and you wish to put JavaScript code in the head of that page alone, How would you go about it?

    You place a  ContentPlaceHolder in the MasterPage as that does not seem to be working for me...

    Hi DJOuk,

    Based on my experience, we can assign value to InnerHtml of the property of "Header"  to solve this problem, see the following code,

       protected void Page_Load(object sender, EventArgs e)
        {
            Page.Master.Page.Header.InnerHtml = @"
               <script language='javascript' type='text/javascript'>
                  function Test(){
                    alert('OK');        
                  }
               </script>";
        }
     
    Hong-Gang Chen
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Idea [Idea] Re: Masterpages & JavaScript

    11-04-2008, 4:54 AM
    Answer
    • Member
      436 point Member
    • warnajith
    • Member since 11-12-2007, 6:11 AM
    • Sri Lanka
    • Posts 63

    Hi,

    .Net has capability to add javascript to your page while it's parsing.

     

    ClientScriptManager
     has lots of methods to gain this feature. if you want to add javascript to the page head use  
    ClientScriptManager.RegisterStartupScript(type,string,string,bool)

     try this link .

     

    Kelum W. Ganegoda (MCSD.NET)
    :: Please mark as answer if you got it right ::
Page 1 of 1 (5 items)