Javascript in widget?

Posted by Community Admin on 03-Aug-2018 01:10

Javascript in widget?

All Replies

Posted by Community Admin on 07-Dec-2010 00:00

Hello, what is the best practice to add javascript when creating a widget?


I tried the following:

1, add javascript directly to the template (ascx) file -- script section is removed when the control is rendered.
2, add javascript through builtin "javascript" widget -- this means adding javascript and content separately, creating more work and making difficult to check into TFS.

Thanks,
Bill

Posted by Community Admin on 07-Dec-2010 00:00

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>


Best wishes,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 07-Dec-2010 00:00

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>

But it's giving me an NullReferenceException:

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



Posted by Community Admin on 07-Dec-2010 00:00

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);
    

The problem is that ResourceLinks is looking inside Telerik.Sitefinity.Resources.Reference which is the default configuration value and your script is not found there

Generally you can change the ResourcesAssemblyInfo in Application_Start of the Global asax but this would result in an issue with the other resources.

Type assemblyInfo = Telerik.Sitefinity.Configuration.Get<ControlsConfig>();
assemblyInfo.ResourcesAssemblyInfo = "YOUR ASSEMBLY HERE"

Best wishes,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 07-Dec-2010 00:00

Or, to take a step back, is it possible to inject javascript code to $(document).ready() block?


Edit: oh, I did not see your response, I will try that first!

Posted by Community Admin on 07-Dec-2010 00:00

Javascript isn't stripped out the widget I created.

Posted by Community Admin on 07-Dec-2010 00:00

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

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 07-Dec-2010 00:00

This is funny, I just tried to embed javascript into the template again and it worked! Must have missed something when I first tried.


Anyways, thanks for all the help!

This thread is closed