+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Javascript in widget?

Javascript in widget?

8 posts, 1 answered
  1. Bill Y
    Bill Y avatar
    22 posts
    Registered:
    04 Nov 2010
    07 Dec 2010
    Link to this post
    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
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    07 Dec 2010
    Link to this post
    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
  3. Bill Y
    Bill Y avatar
    22 posts
    Registered:
    04 Nov 2010
    07 Dec 2010
    Link to this post
    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



  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    07 Dec 2010
    Link to this post
    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
  5. Bill Y
    Bill Y avatar
    22 posts
    Registered:
    04 Nov 2010
    07 Dec 2010
    Link to this post
    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!
  6. Lorne
    Lorne avatar
    38 posts
    Registered:
    04 Nov 2010
    07 Dec 2010
    Link to this post
    Javascript isn't stripped out the widget I created.
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    07 Dec 2010
    Link to this post
    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
    Answered
  8. Bill Y
    Bill Y avatar
    22 posts
    Registered:
    04 Nov 2010
    07 Dec 2010
    Link to this post
    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!
8 posts, 1 answered