Forums

Skip Navigation LinksHome / Developer Network / Forums / Sitefinity 3.x: Developing with Sitefinity > JavaScript Files

JavaScript Files

  • Posted on Aug 14, 2009 (permalink)

    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

     

     

     

    Reply

  • Parvan Parvan admin's avatar

    Posted on Aug 17, 2009 (permalink)

    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.

    Reply

  • Posted on Aug 17, 2009 (permalink)

    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

    Reply

  • Answer Phani avatar

    Posted on Aug 17, 2009 (permalink)

    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.

    Reply

  • Answer Parvan Parvan admin's avatar

    Posted on Aug 18, 2009 (permalink)

    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.

    Reply

  • Posted on Aug 18, 2009 (permalink)

    Thanks for you help guys that sorted it.

    Reply

  • Register for webinar
Skip Navigation LinksHome / Developer Network / Forums / Sitefinity 3.x: Developing with Sitefinity > JavaScript Files