This is a migrated thread and some comments may be shown as answers.

Clicking X before Window finishes loading...causes error

1 Answer 31 Views
Window
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 08 Nov 2013, 01:09 PM
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');
  }

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 08 Nov 2013, 01:39 PM
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.
Tags
Window
Asked by
Peter
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or