More in this section

Forums / Developing with Sitefinity / Referencing scripts from MVC Views

Referencing scripts from MVC Views

3 posts, 0 answered
  1. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    18 Oct 2012
    Link to this post
    I am sure many have asked, but it is hard to search the forums since the new site launched.

    I am familiar with how to register jQuery and other scripts in masterpages with the JavascriptEmbedControl and ResourceFileLink controls, but how does that process translate to when you are using MVC?

    I have a view that I will be adding a slideshow player to and I want to load jQuery only if it has not been loaded already.  I got it to load with the following in the view.

    <script src="@Url.Content("//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js")"></script>

    It loads, but I would love to know how I can target when/where this loads.  Like if I want it in the head section or not to load if I am in the backend where jQuery has already been loaded.

    I supposed I could just load this on the masterpage like always, but I hate to load something that is only going to be needed for one page globally like that.
  2. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    18 Oct 2012
    Link to this post
    This is what I have ended up with so far and it works both on the frontend and backend without any script errors, but it feels too easy.  

    <link type="text/css" rel="stylesheet" href="/Sitefinity/WebsiteTemplates/Public/App_Themes/Public/Styles/orbit.css" />
    <script src="@Url.Content("//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js")"></script>
    <script src="@Url.Content("/Sitefinity/WebsiteTemplates/Public/Scripts/foundation.js")"></script>

  3. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    06 Nov 2012
    Link to this post
    With ASP.MVC, it is *supposed* to be that easy. If it doesn't work, please update this thread as I'll be doing something like that soon as well. 

    You may wish to include a require.js type of script loader to check if the script exists on the page already so you don't duplicate the download, but that's not necessary if you know that there won't be any duplicate scripts on your page.
3 posts, 0 answered