clientside script does not work when loaded from from external file

9 posts, 1 answers
  1. Alex
    Alex avatar
    26 posts
    Member since:
    Sep 2007

    Posted 27 Sep 2008 Link to this post

    Hello,

    I tried moving all of the JavaScript functions out to an external file and reference it like this:

    <head id="Head1" runat="server"
    ... 
    <script type="text/javascript" src="MyScriptFile.js"</script> 
    </head> 

    and none of the functions work. The script file is in my web project directory. When I examine with IE Developer Toolbar, I see error messages with my vars set to null when the functions are called:

    for example:
    tree is null

    However, when I include the script inside my aspx page, everything works.
    What am I doing wrong?

    Best Regards,
    Alex
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 29 Sep 2008 Link to this post

    Hi Alex,

    How the "tree" variable is initialized?

    All the best,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Alex
    Alex avatar
    26 posts
    Member since:
    Sep 2007

    Posted 29 Sep 2008 Link to this post

    Hi Vlad,

    This is my code:

    function OnKeyPressingInTree(sender, eventArgs) 
           // var node = eventArgs.get_node();   
           // var key = eventArgs.get_domEvent().keyCode;    
            
           var key = eventArgs.get_domEvent().keyCode; 
            
           if(key == "40")  // down-arrow 
           {   
              var tree = $find("<%= RadTreeView1.ClientID %>"); 
              var node = tree.get_selectedNode(); 
              var nextNode = node.get_nextNode(); 
               

    and the .aspx tree definition:

                               <telerik:RadTreeView ID="RadTreeView1" runat="server" Style="position: relative; 
                                    top: 0px; left: 0px;" AllowNodeEditing="True" BorderColor="Black" EnableDragAndDrop="True" 
                                    OnNodeDrop="RadTreeView1_NodeDrop" Skin="Telerik" BorderStyle="Solid" BorderWidth="1px" 
                                    MultipleSelect="True" Width="100%" OnNodeDataBound="RadTreeView1_NodeDataBound" 
                                    OnClientNodeDragStart="OnClientNodeDragStartHandler" OnClientMouseOver="onMouseOver" 
                                    OnClientMouseOut="onMouseOut" OnClientNodeClicked="OnClientNodeClickedHandler" 
                                    OnClientNodePopulating="nodePopulating" TabIndex="1" Height="100%" OnClientKeyPressing="OnKeyPressingInTree"
                                    <WebServiceSettings Path="MyWebService.asmx" Method="GetTreeViewNodes" /> 
                                    <ContextMenus> 
     
    ... 


    This is the call stack from Firebug:

    tree is null 
    OnKeyPressingInTree(Object _element=div#RadTreeView1.RadTreeView, Object _cancel=false _node=Object _domEvent=Object)MyScriptFile.js (line 129) 
    createCallback()(Object _element=div#RadTreeView1.RadTreeView, Object _cancel=false _node=Object _domEvent=Object)Telerik....a819163f9 (line 6) 
    raiseEvent()("keyPressing", Object _cancel=false _node=Object _domEvent=Object)Telerik....a819163f9 (line 745) 
    _raiseCancelEvent()("keyPressing", Object _element=li.rtLI _parent=Object, Object type=keydown rawEvent=Event keydown altKey=false)Telerik....a819163f9 (line 7766) 
    _onKeyDown()(Object type=keydown rawEvent=Event keydown altKey=false)Telerik....a819163f9 (line 7387) 
    _onDomEvent()(Object type=keydown rawEvent=Event keydown altKey=false)Telerik....a819163f9 (line 4959) 
    createCallback()()Telerik....a819163f9 (line 6) 
    createCallback()(keydown charCode=0keyCode=40)Telerik....a819163f9 (line 6) 
    [Break on this error] var node = tree.get_selectedNode(); 


    The down arrow functionality works when I inline my scripts inside the .aspx page.

    I commented out the other script errors which were reporting prior to the "tree is null" to isolate this error.

    Best Regards,
    Alex
  5. Answer
    Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 29 Sep 2008 Link to this post

    Hi Alex,

    This line will work only if you use it on the page:
    var tree = $find("<%= RadTreeView1.ClientID %>");

    Kind regards,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Alex
    Alex avatar
    26 posts
    Member since:
    Sep 2007

    Posted 29 Sep 2008 Link to this post

    Vlad,

    Yes, you are right, of course - the <% %> tags... Is there an equivalent functionality alternative I can use for that so that I could put the script in a separate file?

    Thank you,
    Alex
  7. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 30 Sep 2008 Link to this post

    Hi Alex,

    You can put everything else in the external script file except "tree" variable.

    Kind regards,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. Alex
    Alex avatar
    26 posts
    Member since:
    Sep 2007

    Posted 30 Sep 2008 Link to this post

    Vlad,

    I found out that you don't have to use the asp.net surround tags in the external script: 

    var tree = $find("RadTreeView1");

    This worked for me.

    Thank you for your help,
    Alex
  9. Axe
    Axe avatar
    88 posts
    Member since:
    Feb 2007

    Posted 01 Oct 2008 Link to this post

    Keep in mind this won't work if the control is placed inside another control using INamingContainer interface eg. MasterPage/ContentPage, WebUserControl or most composite controls such as RadGrid. If you only have the control on an .aspx page with no master page then you are safe.
  10. Alex
    Alex avatar
    26 posts
    Member since:
    Sep 2007

    Posted 01 Oct 2008 Link to this post

    Thank you for pointing this out. Do you know of a work-around?
    What if I give the controls unique names, i.e. containerRadGrid1, radGrid1, etc..?

    Best Regards,
    Alex
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017