using jquery in sitefinity html and mvc widgets
Hi
Is there a definitive way to reference jQuery from within MVC widgets, and from javascript in script widgets please?
NOTE: Some of my pages just have javascript, others just MVC widgets that need jQuery, others have both.
Sorry to ask but I am seeing a lot of conflicting information.
Thanks,
Tom.
Hi Tom,
There are a lot of ways you can add JQuery to your pages and MVC widgets. Let us first review the pages case.
The best practices for adding JQuery to Sitefinity is the ResourceLinks control and the JavaScriptEmbed control. The controls are similar in function with the difference that the JavaScriptEmbed control has the ScriptEmbedPosition attribute that allows you to place the script reference either in the body or the head of the page. Here is an example:
<%@ Register TagPrefix="sitefinity" Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.PublicControls"%>
<
sitefinity:JavaScriptEmbedControl
ID
=
"script1"
runat
=
"server"
ScriptEmbedPosition
=
"BeforeBodyEndTag"
Url
=
"~/Sitefinity/WebsiteTemplates/Library/App_Themes/Library/Global/Scripts/jquery-ui-1.9.2.min.js"
/>
<
script
src
=
"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"
></
script
>
public
ActionResult Index()
var styleSheetManager = SitefinityStyleSheetManager.GetCurrent(((System.Web.UI.Page)
this
.HttpContext.CurrentHandler));
if
(styleSheetManager !=
null
)
styleSheetManager.StyleSheetsRendered +=
new
EventHandler(
this
.styleSheetManager_StyleSheetsRendered);
return
View(
"Default"
);
private
void
styleSheetManager_StyleSheetsRendered(
object
sender, EventArgs e)
var script =
new
JavaScriptEmbedControl();
script.ScriptEmbedPosition = Telerik.Sitefinity.Web.UI.PublicControls.Enums.ScriptEmbedPosition.Head;
script.Url =
"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"
;
((System.Web.UI.Page)
this
.HttpContext.CurrentHandler).Header.Controls.Add(script);