1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Page-Level Start Up Scripts

Page-Level Start Up Scripts

2 posts, 0 answered
  1. Glenn Boothe
    Glenn Boothe avatar
    1 posts
    Registered:
    02 Jul 2008
    23 Dec 2009
    Link to this post
    Hello,

    I am using Sitefinity Community Edition and I have a need to run specific javascript based on the page a user goes to.  My master page contains a 'head' contentplaceholder but I don't see anywhere I can enter a script into it.  I tried to drag a Generic Content control into the head placeholder and was greeted with an error that says "must be placed in a form tag with runat="server" (or something along those lines).  How might I run page level start up scripts in my Sitefinity installation?

    Thanks
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    24 Dec 2009
    Link to this post
    Hi Glenn Boothe,

    The error message should appear only in edit mode, because the page is null. Once the page is saved you should not see any problems. However you can try a different way, by creating just a user control that adds the js to your head tag.

    Sample code:

    HtmlGenericControl addJs = new HtmlGenericControl("script");
    addJs .Attributes.Add("type", "text/javascript");
    addJs .Attributes.Add("src", "HERE PATH TO YOUR JS");
    this.Page.Header.Controls.Add(addJs);

    or inject the code directly

    HtmlGenericControl addJs= new HtmlGenericControl("script");
    addJs.Attributes.Add("type", "text/javascript");
    addJs.InnerHtml = "alert('Hello');";
    this.Page.Header.Controls.Add(addJs);


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
2 posts, 0 answered