Welcome to Dream.In.Code
Getting Help is Easy!

Join 107,663 Programmers for FREE! Ask your question and get quick answers from experts. There are 991 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Problems with innerHTML property

 
Reply to this topicStart new topic

Problems with innerHTML property

Mike007
post 12 Jun, 2008 - 12:19 AM
Post #1


D.I.C Head

Group Icon
Joined: 30 Aug, 2007
Posts: 200



Dream Kudos: 75
My Contributions


Ok, so i finally ran into troubles with the innerHTML property in FireFox, heard that it has some issues, never really saw any tho until today.

Problem: when using innerHTML as such
jscript

object.innerHTML += "<div id=\"somethingNew\"></div>";
var somethingNew = document.getElementById("somethingNew");
object.innerHTML += "more new stuff";


The somethingNew variable no longer references the somethingNew div tag in the new HTML. This seems odd at first, but maybe when you think about it. When you "add" stuff to innerHTML, you are not really adding anything, you are changing the property and concatenating the old HTML to the new one. By doing this you are forcing the page to be rendered again, but the variable you used points to the old element, which might be the same, but you didn't update it. This thing gave me quite a headache today to be honest.

The solution: for IE don't use innerHTML! It has something which seems to work alot better: insertAdjacentHTML(position, html). This little piece seems to do magic, and not a lot of people know about it for some reason. There one proof that IE can be better than FF lol (don't kill me with its drawbacks IE has i know i've been there almost killed myself then). Now for firefox, fix well, you can do it the dirty way and update the variables after you finished changing the HTML. You can do this just by using this workaround i just found, which i must say looks promising. But this article is pretty old, so there must be something better by now.

Is it better using DOM elements for non-IE browsers to create new HTML? or more specific DIV tags? Is there a better workaround out there?

This post has been edited by Mike007: 12 Jun, 2008 - 12:20 AM
User is offlineProfile CardPM

Go to the top of the page


girasquid
post 12 Jun, 2008 - 05:12 AM
Post #2


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,147



Thanked 6 times

Dream Kudos: 625
My Contributions


Why not use actual DOM elements? Something like this:
jscript

var d = document;
var object = d.createElement('div');
object.id = 'somethingNew';
object.appendChild(d.createTextNode('more new stuff'));

User is offlineProfile CardPM

Go to the top of the page

Mike007
post 12 Jun, 2008 - 12:58 PM
Post #3


D.I.C Head

Group Icon
Joined: 30 Aug, 2007
Posts: 200



Dream Kudos: 75
My Contributions


Yeah this actually works pretty great in FF, as smooth as it can get. But i heard that IE has problems with that sometimes, so i guess i'll use the old insertAdjacentHTML function to be safe for IE.

Thanks alot smile.gif
User is offlineProfile CardPM

Go to the top of the page

level1
post 12 Jun, 2008 - 01:21 PM
Post #4


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 34



Thanked 5 times
My Contributions


while I'm always having to declare variables after the innerhtml has been modified, I think it is worth the increased performance over the DOM method. read this link to see what I'm talking about:

http://www.quirksmode.org/dom/innerhtml.html#t10
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 12 Jun, 2008 - 02:50 PM
Post #5


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,147



Thanked 6 times

Dream Kudos: 625
My Contributions


It's true that in a lot of cases, innerHTML is faster - but from a cross-browser safety point of view, DOM is probably safer - I don't think I've ever encountered an issue with something in the DOM that Firefox did and IE didn't - although I have encountered that issue with innerHTML before(try setting the innerHTML of a tr - FF will let you, and IE won't. IE's is actually the valid behavior in this case).
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/29/08 10:17PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month