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

Forums / Developing with Sitefinity / Add Javascript Files via MasterPage

Add Javascript Files via MasterPage

3 posts, 0 answered
  1. Isaac
    Isaac avatar
    63 posts
    Registered:
    01 Dec 2008
    07 Aug 2011
    Link to this post
    Greetings,

    I have a MasterPage that maintains a collection of javascript files, the MasterPage has a method something like this:

    public addJavascript(String url) {
       //see if its already in the list, if not add it
       _listOfJavascript.Add(url);
    }

    In the MasterPage.PreRender() event, I loop through _listOfJavascript and add the appropriate control to a PlaceHolder control in the HtmlHead object.

    ------------

    I'm attempting to create UserControls which have fields to add path to includes javascript files, this does not work in cmseditmode because in this mode, it appears all the MasterPage events fire before any of the UserControl events.

    I realize within the UserControl I can add my javascript files via a this.Page.Header.Controls.Add() call. However, this solution will only work if there are not multiple instances of the same UserControl on the page, otherwise the javascript file will get added twice causing problems -- which is why I want to utilize the addJavascript() method in my MasterPage.

    Is there some other event that I can bind in my UserControl to fire before MasterPage.PreRender() in cmseditmode, or another way to accomplish my goal.
  2. Isaac
    Isaac avatar
    63 posts
    Registered:
    01 Dec 2008
    09 Aug 2011
    Link to this post
    Nothing on this?

    Is my design idea incorrect as far as adding css & javascript files via a usercontrol?

    Lets say I wanted to make a slideshow usercontrol, and I wanted to support multiple instances of that working on the same page?  I also only want the appropriate css/javascript files loaded if there is an instance of that usercontrol on the page.

    ((MyMasterPage)this.Page.Master).addJavaScript("/shared/nifty-slideshow.js");
    ((MyMasterPage)this.Page.Master).addStyleSheet("/shared/nifty-slideshow.css");

    Seems to me to be a good solution.

    Args!!! =(

  3. Isaac
    Isaac avatar
    63 posts
    Registered:
    01 Dec 2008
    10 Aug 2011
    Link to this post
    I figured this out. I'm an idiot.
Register for webinar
3 posts, 0 answered