Referencing scripts from MVC Views

Posted by Community Admin on 04-Aug-2018 20:27

Referencing scripts from MVC Views

All Replies

Posted by Community Admin on 18-Oct-2012 00:00

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.

Posted by Community Admin on 18-Oct-2012 00:00

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>

Posted by Community Admin on 06-Nov-2012 00:00

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.

This thread is closed