开发者

After upgrading to jQuery 1.6.2, globalEval throws an error when trying to execute javascript on page

开发者 https://www.devze.com 2023-03-28 12:47 出处:网络
I upgraded from jQuery 1.4.2 to 1.6.2 and now I get error(in IE). I have JavaScript on the page that gets executed by jQuery globalEval() function

I upgraded from jQuery 1.4.2 to 1.6.2 and now I get error(in IE). I have JavaScript on the page that gets executed by jQuery globalEval() function

// Evaluates a script in a global context
// Workarounds based on findings by Jim Driscoll
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
globalEval: function( data ) {
    if ( data && rnotwhite.test( data ) ) {
        // We use execScript on Internet Explorer
        // We use an anonymous function so that context is window
        // rather than jQuery in Firefox
        ( window.execScript || function( data ) {
            window[ "eval" ].call( window, data );
        } )( data );
    }
},

In IE the call throws exception:

"Error: Could not complete the operation due to error 80020101."

The data parameter that get executed is javascript variables on page surrounded by <!-- -->

<!-- 
var id = \"ctrl90900\";

var url = \"myur.com/blah.html\";

-->

I'm using IE9, and jQuery 1.6.2开发者_如何学C Not sure why this would cause an error.


If there is any error at all in a script passed to execScript, as far is Internet Explorer is concerned, it will report a 80020101 instead of the original error.

So, also check for missing semicolons and JS features not supported by IE.

For short code passages, i found the most effective debugging technique was to comment out parts of code and see if the error still turns up. If it doesn't, examine the code block that was just commented out for above errors.


It might be the commenting of the code, which is invalid JavaScript and unnecessary in this day and age.

You can strip it out with this regex...

$.globalEval(str.replace(/<!--\s*([\s\S]*?)\s*-->/, '$1'));

jsFiddle.


Just wanted to add a bit to this -- I was getting an error 80020101 whenever in an AJAX-called PHP script that included JavaScript. The script inside the PHP file was also breaking somehow, and refusing to render any of the script in the designated div (other elements of the PHP came through, but nothing inside the JavaScript -- I was trying to draw a chart using HighChart).

Turns out there was an undefined PHP function inside the JavaScript in the included PHP file (used to echo some text). I found this using Firebug.

So bottom line, make sure you don't have any undefined functions inside code coming back via AJAX.

0

精彩评论

暂无评论...
验证码 换一张
取 消