Signpost
Signpost

Reputation: 17

Suppress JavaScript error on page for specific condition

I am getting a javascript error "Invalid argument on Line: 2 Char: 141544 in sp.ui.rte.js" on a SharePoint development. This appears to be a known issue from google within the SharePoint js files - http://wss.boman.biz/Lists/Posts/Post.aspx?List=c0143750-7a4e-4df3-9dba-8a3651407969&ID=69 After analysing the impact I decided that rather than changing the js in the SharePoint 14 hive I want to suppress the error message for just this error. I was trying to do the following:

ClientScriptManager cs = Page.ClientScript;

            //Check to see if the startup script is already registered.
            if (!cs.IsStartupScriptRegistered("Alert"))
            {
                StringBuilder cstext1 = new StringBuilder();

                cstext1.Append("<script type=text/javascript> window.onerror = function (msg, url, num) {return true;} </");
                cstext1.Append("script>");

                cs.RegisterStartupScript(this.GetType(), "Alert", cstext1.ToString());
            }

The problem is this will suppress all js errors on the page not just the sp.ui.rte.js ones. Is it possible to do a string search on the URL (http://SHAREPOINT/_layouts/sp.ui.rte.js?rev=uY%2BcHuH6ine5hasQwHX1cw%3D%3D - where the only consistent value between sites will be /_layouts/sp.ui.rte.js? ) to just search and suppress this exact error? Thanks for any help!

Upvotes: 2

Views: 2170

Answers (2)

tuck
tuck

Reputation: 623

So far this has been the most successful fix I have found and currently using:

function fixRTEBug() {
    // This Fix for parentElement bug in RTE should survive Service Packs and CU's
    function SubstituteRTERangeParentElement() {
        var originalRTERangeParentElement = RTE.Range.prototype.parentElement;
        RTE.Range.prototype.parentElement = function () {
            try {
                originalRTERangeParentElement();
            } catch (e) { }
        }
    }
    SubstituteRTERangeParentElement();
}

ExecuteOrDelayUntilScriptLoaded(fixRTEBug, "sp.ui.rte.js");

Upvotes: 1

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22421

Use try/catch block around code in JS. If message matches something you want to ignore, just do nothing. Propagate everything else.

Your original approach with .onerror would work too if you change it to analyze message and propagate everything not matching ignored string as well.

Upvotes: 1

Related Questions