Search code examples
asp.netasp.net-ajaxupdatepanelasp.net-3.5

JavaScript error when using ASP.NET UpdatePanel


I'm using an UpdatePanel to swap the ActiveView of a MultiView.

In IE6, 7 and 8 and Chrome 7 I get a JavaScript error when the UpdatePanel returns. In Firefox 3.6.1 there is no error reported (in the error console or in Firebug).

The error is on line 3621 of ScriptResource.axd

function Sys$_ScriptLoader$_loadScriptsInternal() {
    var session = this._currentSession;
    if (session.scriptsToLoad && session.scriptsToLoad.length > 0) {
        var nextScript = Array.dequeue(session.scriptsToLoad);
        var scriptElement = this._createScriptElement(nextScript);

        if (scriptElement.text && Sys.Browser.agent === Sys.Browser.Safari) {
            scriptElement.innerHTML = scriptElement.text;
            delete scriptElement.text;
        }            
        if (typeof(nextScript.src) === "string") {
            this._currentTask = new Sys._ScriptLoaderTask(scriptElement, this._scriptLoadedDelegate);
            this._currentTask.execute();
        }
        else {
            var headElements = document.getElementsByTagName('head');
            if (headElements.length === 0) {
                 throw new Error.invalidOperation(Sys.Res.scriptLoadFailedNoHead);
            }
            else {
line 3621:        headElements[0].appendChild(scriptElement);
            }


            Sys._ScriptLoader._clearScript(scriptElement);
            this._loadScriptsInternal();
        }
    }
    else {
        this._stopSession();
        var callback = session.allScriptsLoadedCallback;
        if(callback) {
            callback(this);
        }
        this._nextSession();
    }
}

Uncaught SyntaxError: Unexpected number

Chrome's Developer tools show that headElements is a NodeList with one element and that scriptElement is an HTMLScriptElement NodeList and ScriptElement

What could be the cause of this error? Why is it only apparent in IE and Chrome, and not Firefox?


Solution

  • There was an error in the script that I was outputting in the ScriptManager.RegisterStartupScript. This was causing a runtime error as the browser parsed the JavaScript when the <script> tag was added to the DOM, as suggested by @Rahul in the comments.