Javascript in widget?
Hello, what is the best practice to add javascript when creating a widget?
Hi Bill,
Try using ResourceLinks control to add you script on a page.
<%@ Register TagPrefix="sitefinity" Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI" %>
<
sitefinity:ResourceLinks
id
=
"resourcesLinks"
runat
=
"server"
>
<
sitefinity:ResourceFile
Name
=
"Telerik.Sitefinity.Samples.Scripts.MyScript.js"
Static
=
"true"
/>
</
sitefinity:ResourceLinks
>
Hello Ivan, I tried following code:
<%@ Register TagPrefix=
"sitefinity"
Assembly=
"Telerik.Sitefinity"
Namespace=
"Telerik.Sitefinity.Web.UI"
%>
<sitefinity:ResourceLinks id=
"tweetResource"
runat=
"server"
>
<sitefinity:ResourceFile AssemblyInfo=
"DevFacto.Twitterizer"
Name=
"jquery.tweet.js"
Static=
"true"
/>
</sitefinity:ResourceLinks>
<div id=
"twitterizer"
>blah blah</div>
Object reference not
set
to an instance of an
object
.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace
for
more information about the error and where it originated
in
the code.
Exception Details: System.NullReferenceException: Object reference not
set
to an instance of an
object
.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified
using
the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not
set
to an instance of an
object
.]
Telerik.Sitefinity.Web.UI.ResourceLinks.RegisterResource(ResourceFile resource) +1064
Telerik.Sitefinity.Web.UI.ResourceLinks.OnInit(EventArgs e) +499
System.Web.UI.Control.InitRecursive(Control namingContainer) +140
System.Web.UI.Control.InitRecursive(Control namingContainer) +311
System.Web.UI.Control.AddedControl(Control control, Int32 index) +197
System.Web.UI.ControlCollection.Add(Control child) +79
Telerik.Sitefinity.Web.UI.SimpleView.CreateChildControls() +96
System.Web.UI.Control.EnsureChildControls() +102
System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
Hello Bill,
Try using the code below.
HtmlGenericControl control = new HtmlGenericControl();
string url = string.Empty;
url = Page.ClientScript.GetWebResourceUrl(typeof(HERE SET THE TYPE OF YOUR CUSTOM CONTROL), "EMBEDDED JS RESOURCE");
if (!String.IsNullOrEmpty(url))
control.TagName = "script";
control.Attributes.Add("src", url);
control.Attributes.Add("type", "text/javascript");
this.Page.Header.Controls.Add(control);
Or, to take a step back, is it possible to inject javascript code to $(document).ready() block?
Javascript isn't stripped out the widget I created.
Hello,
Adding just a simple javascript alert code is not stripped for me as well.HtmlGenericControl
will inject the embedded resource in the head tag of the page. ResourceLinks
does the same.
Greetings,
Ivan Dimitrov
the Telerik team
This is funny, I just tried to embed javascript into the template again and it worked! Must have missed something when I first tried.