More in this section

Forums / Developing with Sitefinity / JavaScript Files

JavaScript Files

6 posts, 2 answered
  1. Andrew
    Andrew avatar
    202 posts
    Registered:
    05 Jun 2009
    14 Aug 2009
    Link to this post
    What's the best way to add javaScript Files to the system.
    I have created a filder called js and put a file in there.
    Now i need to include it in a user control.
    However using the usual:

    <script src="../../js/HISCalc.js" type="text/javascript"></script>

     


    Does not work, since the user control will be in a different page, ie different place in the folder structure.

    What would be the std way of doing this in sitefinity?

    Thanks

    A

     

     

     

  2. Parvan
    Parvan avatar
    151 posts
    Registered:
    24 Sep 2012
    17 Aug 2009
    Link to this post
    Hi Andrew,

    Thank you for using our services.
    Try to use an absolute url - an url that describes the path to the file starting from the root of the web site:
    Option 1:
    <script src="/yourSiteVirtualDir/js/HISCalc.js" type="text/javascript"></script>
    Option 2:
    <script src="~/js/HISCalc.js" runat="server" type="text/javascript"></script>

    In the examples I suppose that the js folder is in the root of your web site.

    I hope this helps you.

    Regards,
    Parvan
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  3. Andrew
    Andrew avatar
    202 posts
    Registered:
    05 Jun 2009
    17 Aug 2009
    Link to this post

    Hi Parvan

    I am afraid that does not quite help me.
    Option one will only work as long as the app stays in the same place. if it were to be moved then the folder structure might be different and it immediately stop working.

    Option two will not work because of the runat="server" tag. It tries to compile the file and fails.

    A

  4. Phani
    Phani avatar
    7 posts
    Registered:
    22 Apr 2009
    17 Aug 2009
    Link to this post
    Try it this way

    <script src="<%=applicationPath %>Res/Scripts/jquery-1.3.2.min.download.js" type="text/javascript"</script> 

    in the code behind

    protected void Page_Load(object sender, EventArgs e) 
        { 
            applicationPath = Request.ApplicationPath; 
            if (!applicationPath.EndsWith("/")) 
                applicationPath += "/"; 
        } 

    This should solve your problem.Dont give the Script tag inthe HEAD section, because in the head section Server controls or variables wont behave as expected.
    Answered
  5. Parvan
    Parvan avatar
    151 posts
    Registered:
    24 Sep 2012
    18 Aug 2009
    Link to this post
    Hi Phani,

    The solution proposed from Phani is very good.
    I am proposing a modification of the code (the code functionality is the same):
    <script src="<%=HttpContext.Current.Request.ApplicationPath.EndsWith("/") ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/" %>Res/Scripts/jquery-1.3.2.min.download.js" type="text/javascript"</script>  

    I hope this helps you.

    Greetings,
    Parvan
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
    Answered
  6. Andrew
    Andrew avatar
    202 posts
    Registered:
    05 Jun 2009
    18 Aug 2009
    Link to this post
    Thanks for you help guys that sorted it.
Register for webinar
6 posts, 2 answered