View Full Version : Syntax Error loading UpdateManager with scripts true
marklar
7 Mar 2007, 1:42 PM
I am getting a syntax error reading any HTML file with comments around the script. I have no control over this becuase the server puts them there and I am sure others will have the same output and run into this issue.
Is there something simple I can do to get around this issue?
I am providing details on how to recreate in hopes for an answer.
How To Recreate
Create 2 html files .....
1) With Comments (with.htm)
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
function doit() {
go();
}
// -->
</script>
</head>
<body>
Test Page
</body>
</html>
2) Without Comments (without.htm)
<html>
<head>
<script language="JavaScript" type="text/javascript">
function doit() {
go();
}
</script>
</head>
<body>
Test Page
</body>
</html>
Code to recreate error
This code gets a syntax error
var umerr = new Ext.UpdateManager("container");
umerr.update({
url: "with.htm",
scripts: true})
This code gets no syntax error
var um = new Ext.UpdateManager("container");
um.update({
url: "without.htm",
scripts: true})
Notes:
The syntax error is located in the update function of El. Stripping out the '<!-- ' caused the error to go away and load correctly, showing it was that part of the page conflicting with the code.
I am a green apple here - While stripping out the comments from html fixed the issue, I am looking for the obvious thing I am over looking. :)
Thank you all and wonderful tool set. When I get up to speed I'll contiribute.
tryanDLS
7 Mar 2007, 1:49 PM
This has been discussed before - see this
http://www.jackslocum.com/forum/viewtopic.php?t=1442
marklar
7 Mar 2007, 2:08 PM
And since I can not change the source of the server output, I must use an overridden version.
Can anyone provide help on how to strip out the content of html and where?
tryanDLS
7 Mar 2007, 3:36 PM
I would think the place to start would be UpdateManager.processSuccess. SortTypes.asText uses a regex to strip HTML tags - you might use this as a base for what you need to strip from your response.
marklar
7 Mar 2007, 4:56 PM
The code in UpdateManager.processSuccess.SortTypes.asText is never hit before the syntax error - can you provide a bit more detail as to how to accomplish this?
I provided an easy way to re-create the problem. Is there no easy logical way to fix this?
If not, it sure would save us quite a bit of work here if we could have the simple fix of changing this line:
match[1]);
to something like:
try{eval(match[1]);}catch(e){}
I can see why there is not more error checking - more code is slower load. Here we are talking 15 characters for a fix, would be nice to have an exception here.
Thanks for your responses and again, great tool I am trying hard to use the base as is without mods.
brian.moeskau
7 Mar 2007, 6:27 PM
I may be wrong, but I think Tim was referring you to that function as an example to look at for implementing your own regex.
Regarding your proposed "fix" -- in general, it is bad to swallow errors like that, especially in framework code. In fact, there are spots in YUI (Connection manager comes to mind) that do exactly that, and when your code simply returns nothing with no error and you spend a bunch of time debugging manually until you track it down to an empty catch block, that is extremely annoying.
Regarding overhead, I think Jack was referring less to the byte count and more to the overhead of adding extra logic to the regex that would execute every time to filter out comment characters that should not be in there to begin with. The point was that as it's an exception case, it should be handled through user-supplied logic.
marklar
7 Mar 2007, 7:03 PM
I have no problem with the fact that this should be fixed by user side logic. The problem I am having is figuring out how to do this. Apparently is it not that easy or someone would say .. here add this line to here and it is fixed - recreating it sure is easy.
I am using the framework at a high level - using the update function of the update manager. My Lotus Notes server does not spit out perfect code and I have no control over this. To fix a syntax error deep in the code, I have to pull off something so complicated no one can explain it to me and implement this in all my calls to Lotus Notes forms. I imagine there is something simple I am missing. I'll keep learning and will provide details when I understand it enough to explain it.
Thanks again for active posts and a great tool - this is a minor nit, sure I'll find a solution. :)
briandunnington
7 Mar 2007, 7:13 PM
here is a quick (and dirty?) way to fix your problem that you can simply cut-and-paste into your code. it essentially overrides the update method on the UpdateManager with a version that is exactly the same, except for this line:
eval(match[1].replace(""));
of course, if you have any other spacing or variation in your comments, it wont work. a regex would be better, but you wanted a quick fix.
/* this is here to handle html comment tags in script block */
YAHOO.ext.Element.prototype.update = function(html, loadScripts, callback){
if(typeof html == 'undefined'){
html = '';
}
if(loadScripts !== true){
this.dom.innerHTML = html;
if(typeof callback == 'function'){
callback();
}
return this;
}
var id = YAHOO.util.Dom.generateId();
var dom = this.dom;
html += '<span id="' + id + '"></span>';
YAHOO.util.Event.onAvailable(id, function(){
var hd = document.getElementsByTagName("head")[0];
var re = /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/img;
var srcRe = /\ssrc=([\'\"])(.*?)\1/i;
var match;
while(match = re.exec(html)){
var srcMatch = match[0].match(srcRe);
if(srcMatch && srcMatch[2]){
var s = document.createElement("script");
s.src = srcMatch[2];
hd.appendChild(s);
}else if(match[1] && match[1].length > 0){
eval(match[1].replace(""));
}
}
var el = document.getElementById(id);
if(el){el.parentNode.removeChild(el);}
if(typeof callback == 'function'){
callback();
}
});
dom.innerHTML = html.replace(/(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/img, '');
return this;
}
marklar
7 Mar 2007, 7:54 PM
Very nice - I am using 1.0 Alpha, so with a minor update - I was able to get this working.
Now I just add one more include file with the below mod and presto - fixed.
I now understand how to do this in other areas thanks to your fantastic response.
THANK YOU - THANK YOU - THANK YOU
Here is the code that worked - let me know if you see I did something stupid.
Note: Had to change E. to Ext.lib.Event. in one place - outside that worked like a charm to just copy over the code from the base, plop it in a new file, make the update and add to the includes after the base. I am not worried about cases where it is not an exact match - because the Notes Server sends out the same way and that is the only concern here.
/* MOD - To handle Notes Server Non Standard Response */
Ext.Element.prototype.update = function(html, loadScripts, callback){
if(typeof html == "undefined"){
html = "";
}
if(loadScripts !== true){
this.dom.innerHTML = html;
if(typeof callback == "function"){
callback();
}
return this;
}
var id = Ext.id();
var dom = this.dom;
html += '<span id="' + id + '"></span>';
Ext.lib.Event.onAvailable(id, function(){
var hd = document.getElementsByTagName("head")[0];
var re = /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/img;
var srcRe = /\ssrc=([\'\"])(.*?)\1/i;
var match;
while(match = re.exec(html)){
var srcMatch = match[0].match(srcRe);
if(srcMatch && srcMatch[2]){
var s = document.createElement("script");
s.src = srcMatch[2];
hd.appendChild(s);
}else if(match[1] && match[1].length > 0){
eval(match[1].replace(""));
}
}
var el = document.getElementById(id);
if(el){el.parentNode.removeChild(el);}
if(typeof callback == "function"){
callback();
}
});
dom.innerHTML = html.replace(/(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/img, "");
return this;
}
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.