IE and innerHTML problem

I

Ivan S

I'm getting long and multiline string (HTML) by AJAX and than use
element's innerHTML property to display it (I know that innerHTML is
not standard, if there is a better method, suggest it to me :) ).
Everything is working well in every browser I've tested, but not in
IE. I'm getting error: "Unterminated string constant". I've done some
Google homework, it seems that newline charaters are messing things
up ... and I've tried some solutions, but none of them worked (I used
regular expresion to replace newline charaters). Any suggestions?



Tnx




Ivan
 
T

Thomas 'PointedEars' Lahn

Ivan said:
I'm getting long and multiline string (HTML) by AJAX and than use
element's innerHTML property to display it (I know that innerHTML is
not standard, if there is a better method, suggest it to me :) ).

W3C DOM Level 1+ Core and HTML.
Everything is working well in every browser I've tested, but not in
IE. I'm getting error: "Unterminated string constant". I've done some
Google homework, it seems that newline charaters are messing things
up ...

Yes, unescaped line terminators must not occur in a string literal;
according to ECMA-262, even escaped line terminators must not occur there.
and I've tried some solutions, but none of them worked (I used
regular expresion to replace newline charaters). Any suggestions?

<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://jibbering.com/faq/#posting>

Probably you need to replace newlines *server-side* with "\n". (You can't
work client-side with what is syntactically invalid already.)


PointedEars
 
I

Ivan S

W3C DOM Level 1+ Core and HTML.

I've read some info about it, but I don't know what to do if I have
HTML string. To parse it somehow or what?
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://jibbering.com/faq/#posting>

Probably you need to replace newlines *server-side* with "\n".  (You can't
work client-side with what is syntactically invalid already.)

Hm, what is so not smart in my question?




Anyway, thanks for your answer, I'll try to do it on server side.




Ivan
 
T

Thomas 'PointedEars' Lahn

Ivan said:
I've read some info about it, but I don't know what to do if I have
HTML string.

Do not serve an HTML string then. Serve JSON and eval()/parse it
client-side, then build the subtree from the data in memory and use
appendChild() & friends to include it. BTDT.
To parse it somehow or what?

AFAIK that is another possibility. Serve XML and use an XML parser
client-side, then clone it so that you can use appendChild() & friends.
Hm, what is so not smart in my question?

You have posted the error message without the code fragment(s) it is
probably created by, which leaves those who want to help you with either
playing a guessing game (grep for "twenty questions") or skipping your
message for something more interesting.
Anyway, thanks for your answer, I'll try to do it on server side.

You're welcome.


PointedEars
 
I

Ivan S

Do not serve an HTML string then.

Unfortunaty, sometimes modifying someone's script isn't easy at
all. :)
You have posted the error message without the code fragment(s) it is
probably created by, which leaves those who want to help you with either
playing a guessing game (grep for "twenty questions") or skipping your
message for something more interesting.

I thought they wouldn't be useful in this case.



Anyway, I found some more info about a problem after some testing. It
seems that newlines are not causing the problems. IE has a problem
with AJAX calls when using UTF-8 encoding (and non ASCII are present).
When I put different encoding, everything works well (except non ASCII
characters aren't encoded properly, but I can fix that).





Ivan
 
T

Thomas 'PointedEars' Lahn

Ivan said:
Unfortunaty, sometimes modifying someone's script isn't easy at
all. :)

If modifying the generating server-side script so that JSON or other
non-markup data formats are used is not an option, ISTM you are left
with the following alternatives:

1) The generated markup is a Valid X(HT)ML fragment served as
application/xhtml+xml or application/xml or text/xml
(in short: XML media types) and the target markup is
Valid XHTML served as such:

Use a client-side XML parser (built into MSXML and Gecko),
clone, and insert/replace.

2) The generated markup is a Valid HTML fragment served as such:

a) Convert it to XHTML client-side, use an XML parser there,
clone, and insert/replace.

b) Write a client-side HTML parser that builds the subtree
(It is not that hard, using RegExp in a while-loop, and
observing a few DTD rules while creating the DOM objects),
then insert/replace the subtree.

c) Use the `innerHTML' property (with its known side effects).

Also, there's always the possibility to do most of the work server-side
using transparent proxying.
Anyway, I found some more info about a problem after some testing. It
seems that newlines are not causing the problems. IE has a problem
with AJAX calls

There is no such thing as an "AJAX call".
I thought they wouldn't be useful in this case.

Rule of thumb: In order to receive help you should give other people as much
*information* about the problem as possible to increase the chance that this
information contains the parts that these people need in order to help you.

See also *especially* (but not exclusively)
<http://www.catb.org/~esr/faqs/smart-questions.html#beprecise> and the
following section.

BTW: Asking "What is so not smart in my question?" after being pointed to
a tutorial on how to ask smart questions is essentially asking a stupid
question :)
when using UTF-8 encoding (and non ASCII are present).
When I put different encoding, everything works well (except non ASCII
characters aren't encoded properly, but I can fix that).

You should LART the person responding with such junk instead. There needs
to be a

Content-Type: ...; charset=UTF-8

header in the response message if the message body contains characters in
UTF-8 encoding. Everything else must result in guesswork on part of the
user agent (here probably ISO-8859-1, as your "non ASCII characters" are
instead Unicode characters beyond code point U+007F), so you really cannot
blame IE/MSHTML then.

Incidentally, you would not have had that problem with XML (read: an XHTML
fragment) as UTF-8 is one of the default encodings there. Proxying also can
work around that in case the other person acts not as responsible as should
be required.


PointedEars
 
I

Ivan S

If modifying the generating server-side script so that JSON or other
non-markup data formats are used is not an option, ISTM you are left
with the following alternatives:

1) The generated markup is a Valid X(HT)ML fragment served as
   application/xhtml+xml or application/xml or text/xml
   (in short: XML media types) and the target markup is
   Valid XHTML served as such:

   Use a client-side XML parser (built into MSXML and Gecko),
   clone, and insert/replace.

2) The generated markup is a Valid HTML fragment served as such:

   a) Convert it to XHTML client-side, use an XML parser there,
      clone, and insert/replace.

   b) Write a client-side HTML parser that builds the subtree
      (It is not that hard, using RegExp in a while-loop, and
      observing a few DTD rules while creating the DOM objects),
      then insert/replace the subtree.

   c) Use the `innerHTML' property (with its known side effects).

Also, there's always the possibility to do most of the work server-side
using transparent proxying.

Ok, tnx ... this is nice to know. :)
There is no such thing as an "AJAX call".

XMLHttpRequest call to be precise?
Rule of thumb: In order to receive help you should give other people as much
*information* about the problem as possible to increase the chance that this
information contains the parts that these people need in order to help you.

Yes, but I think that doesn't mean giving a lot of useless or
redundant information.
BTW: Asking "What is so not smart in my question?" after being pointed to
a tutorial on how to ask smart questions is essentially asking a stupid
question :)

Well, I think there are no stupid questions (there could be stupid
answers).
I asked you because I didn't know what are you referring to (and
didn't want to guess).
You should LART the person responding with such junk instead.

And LART is?
 There needs to be a

  Content-Type: ...; charset=UTF-8

header in the response message if the message body contains characters in
UTF-8 encoding.

And there is.
PointedEars

Thanks for your help (and a lot of useful informations), I solved the
problem.





Ivan
 
T

Thomas 'PointedEars' Lahn

Ivan said:
XMLHttpRequest call to be precise?

Lose the "call" to be more precise; use XHR as an abbreviation.
XMLHttpRequest (precisely: an implementation of the IXMLHTTPRequest
interface) makes an HTTP *request* and handles the response; nothing
more, nothing less. That you need a number of calls to the methods
of this implementation to do that does not make the whole thing an
"(AJAX) call" in my book. (You might have been mislead by clueless
library authors.)
Yes, but I think that doesn't mean giving a lot of useless or
redundant information.

But in order to know that an information is definitely redundant you have to
be in a position to solve the problem by yourself.
BTW: Asking "What is so not smart in my question?" after being pointed to
a tutorial on how to ask smart questions is essentially asking a stupid
question :)

Well, I think there are no stupid questions [...]

NAK, see below.
And LART is?

That's a stupid question.
And there is.

Curious. How come I have no problem with it?
Thanks for your help (and a lot of useful informations), I solved the
problem.

Are you sure you solved the right one?


PointedEars
 
I

Ivan S

But in order to know that an information is definitely redundant you haveto
be in a position to solve the problem by yourself.

Maybe. And maybe not. If you prefer a lot of information, I'll give it
to you if I'll have JS issues in the future.
That's a stupid question.

No. This is stupid answer. :)
You should answer what LART means because not everybody knows that.
Curious.  How come I have no problem with it?

Good point.
Are you sure you solved the right one?

Yes. But not in a right way. :)
 
J

JR

I'm getting long and multiline string (HTML) by AJAX and than use
element's innerHTML property to display it [...]

I do the same with AJAX, loading an ASP page that creates a table with
212 rows. So far I haven't experienced any problems in IE or whichever
browser I've used.
[...] (I know that innerHTML is
not standard, if there is a better method, suggest it to me :) ).

Okay, innerHTML is not standard but it has been working in any browser
for many years. That makes me feel like innerHTML is a 'de facto'
standard.
Everything is working well in every browser I've tested, but not in
IE. I'm getting error: "Unterminated string constant".

Try to debug your code with Firebug and inspect the HTML markup that
is returned by your XMLHttpRequest object. That might reveal what's
wrong with it.

Cheers,
JR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,099
Latest member
AmbrosePri
Top