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);
}