Clicking X before Window finishes loading...causes error

2 posts, 0 answers
  1. Peter
    Peter avatar
    71 posts
    Member since:
    Mar 2012

    Posted 08 Nov 2013 Link to this post

    We have windows with radgrids and other controls.

    If the user clicks the X before the page has finished loading we get this error:  (in Chrome 30)
    Uncaught TypeError: Cannot read property `1` of null

    This is the code:

    var oWnd = window.$find('Modal' + (window.myModalWindows.length + 1));

      function OnClientBeforeClose(sender, eventArgs) {
        var pageName = oWnd.get_contentFrame().contentWindow.location.pathname.match(/.*\/([^\/\.]+)/i)[1]; <-- Error line  

        if (oWnd.get_contentFrame().contentWindow[pageName + "_onunload"])
          oWnd.get_contentFrame().contentWindow[pageName + "_onunload"]();
        oWnd.get_contentFrame().contentWindow.location.replace('Blank.aspx');
      }
  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    3595 posts

    Posted 08 Nov 2013 Link to this post

    Hi Peter,

    You are attempting to call a function from the content page when closing the RadWindow. If the content page is not fully loaded the function is not available or it cannot do its work and will throw an exception. It is the developer's responsibility to ensure checks (and/or try-catch blocks) are in place to ensure there are no errors.

    What I can suggest is the following:
    - check if the function is available, something like:
    if( oWnd.get_contentFrame().contentWindow &&
         oWnd.get_contentFrame().contentWindow.location &&
         oWnd.get_contentFrame().contentWindow.location.pathname)
    {
       //use the  oWnd.get_contentFrame().contentWindow.location.pathname object
    }

    - add try-catch blocks in the functionality to ensure missing objects will not break your scripts

    - raise a flag in the OnClientPageLoad event that will mean that you can call this functionality. Check for that flag in the OnClientBeforeClose event to see if you can execute the logic. You can reset the flag in OnClientClose

    Regards,
    Marin Bratanov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top