Thursday, December 2, 2010

Sharepoint 2010 Javascript error when adding or editing items in a list using popups

I have discovered a weird problem in Sharepoint 2010 and many of developers have discovered that too. To reproduce the problem follow the following steps:

1. Create new list of any type (Calendar, custom,...etc).
2. browse the list.
3. Click on Add new Item.
4. In the pop up, click on maximize before the page finishes loading (once the maximize button appear). (A).
5. once maximized or once the pop up appears, click on close to close the pop up dialog. (B)

Expected:
for A: the pop up should be maximized.
for B: the pop up should be closed and returns to the list view.

Actual:
For A: Invalid Argument javascript error occurs in SP.UI.RTE.js.
For B: Invalid Argument javascript error occurs in SP.UI.Dialog.js.

I have tried the above scenarios in multiple servers and i can reproduce the the error in all cases. i have tried to resolve the problem but i cannot find any straightforward one. However, i discovered that after the javascript errors appear, the pop up behaves normally so, i decided to try handling that error by writing a custom javascript to handle the error. After that, the javascript errors disappeared and everything is now working very well.

To handle that error, just write a custom javascript function to handle the error using the following code:

//START DIRTY WORKAROUND TO HANDLE THE JS ERROR WHEN CLICKING ADD NEW ITEM OR EDIT AND MAXIMIZE BEFORE WINDOW FINISHS LOADING
//ALSO HANDLES WHEN CLOSING THE POPUP BEFORE FINISHING, IT GIVES JS ERROR.
function noErrorMessages(err, url) {
    // HANDLE MAX THE POPUP ERROR
    if (err.search(/invalid argument/gi) >= 0 && url.search(/sp.ui.rte/gi) >= 0) {
        return true;
    }
    // HANDLES CLOSING THE POPUP
    else if (err.search(/or not an object/gi) >= 0 && url.search(/sp.ui.dialog/gi) >= 0) {
        return true;
    }
    return false;

}
window.onerror = noErrorMessages;

//END DIRTY WORKAROUND

The above javascript code hides the above errors only and behaves normally for the other javascript errors. (use browser default behavior). By adding the above javascript code to the master page or to the page that shows the adding new item or editing existing item, the javascript errors will disappear and everything will behave normally.