standards-compliant equivalent of innerHTML

W

wilq

Hi,

I have an application which uses Javascript ro replace an element's
innerHTML with a string of HTML retrieved from an Ajax call. In some
circumstances I get an "unknown runtime error" from IE which seems to
be a bug (if you google for innerHTML "unknown runtime error" you'll
see what I mean), and I have also had problems with Chrome too.

The crux of the issue seems to come down to the fact that innerHTML is
not part of the W3C DOM Specification, so I am looking for a standards-
compliant way to achieve the same thing.

In my application, both the main HTML document and the HTML fragment
I'm trying to insert are user-supplied content so I am not in control
of them - I need my application to work with any valid HTML content.

The HTML is not necessarily valid XHTML, so I cannot use responseXML
in the ajax call.

at the moment I can only see 3 options, none of which appeal:

1. keep trying to work around problems with individual browsers
(fiddly and no guarantee of success)

2. somehow convert the HTML to XHTML on the server, then I can use
responseXML

3. parse the returned HTML fragment into a DOM tree in javascript
(presumably using js string slicing)

any other thoughts would be gratefully appreciated

Andy

If you dont care about all those things underground you can just
simply instead of:


target.innerHTML = YOURDATA;


try do:

var el=document.createElement('div');
el.innerHTML = YOURDATA;
target.innerHTML="";

while (el.firstChild){
target.appendChild(el.firstChild);
}
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Suppose you are looking for an ECMAScript-compliant solutions, as anything
else would be off-topic here, you should use a DPDA implementation that
uses Regular Expressions, i.e.

var m, rx = /.../, s = "...";

JFTR:

... rx = /.../g ...

is necessary at least or
while ((m = rx.exec(s)))

will blocks your client.


PointedEars
 
T

Thomas 'PointedEars' Lahn

kangax said:
It might be a good idea to take care of non-advancing lastIndex with
zero-length matches in some implementations.

For zero-length matches to occur, the expression needs to allow them.
In this case, it does not need to (in double sense); the shortest string
that needs to be matched is a single character (representing a single-
character text node).


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top