IE7 evalScripts with Prototype throws an error
Thursday, May 22nd, 2008This is yet anther IE bug I have found whilst doing a lot of AJAX stuff with prototype recently. If you make and AJAX request such as AJAX.Update(blah blah and you set evalScripts: true then IE7 will throw a wobbly (great).
Basically if the JS has comments around it then IE blows up. I found this a useful link:
When using Ajax.Updater with the option evalScripts:true like:
new Ajax.Updater( table_id+'tbody', '/someurl', { parameters : { 'table_id':table_id }, evalScripts : true });and response of /someurl contains the following script:
<script language="JavaScript" type="text/javascript"> <!-- alert( 'Hello' ); // --> </script>IE7 throws an error “Syntax error” in line 212 of prototype.js version 1.5.0 in function evalScripts.
If you try a non-commented script like:
<script language="JavaScript" type="text/javascript"> alert( 'Hello' ); </script>it works. Firefox works whether the script is commented or not.
I am currently using JSF and Facelets for my web app that adds the comments around the JS for you - PAIN IN &^%$£
Not got a solution yet for JSF!
UPDATE!!
We tried to change myFaces so that it did not render the comments but thought that it was a bad idea and back tracked!
So the fix had to be in the JavaScript. We added the following to the Prototype class:
exec: function(code) {
if ((code += '').blank()) return;
var script;
if (document.body) {
script = new Element('script', { type:'text/javascript' });
try {
script.appendChild(document.createTextNode(code));
} catch (e) { script.text = code; }
($$('head').first() || $(document.documentElement)).insert(script);
}
else {
var scriptId = '__prototype_exec_script';
document.write('<script id="'+ scriptId +'" type="text/javascript">'+ code +'<\/script>');
script = $(scriptId);
}
script.remove();
script.text = '';
},
And then changed where it evals the scripts to use this method instead. This does mean we cannot easily just upgrade the version of prototype we are using but to be honest I would rather it worked in the first place!