Thursday, June 14

No document.write() Allowed

I have not been doing a good job of posting in here, but honestly, I haven't been doing anything interesting at all. I guess the latest thing I learned was about document.write().

It is NOT ALLOWED in XHTML 1.1 Transitional. It creates code that will not be valid by the W3C. I was unaware when I started so I had a page that used it. I was using it for a section that randomly loaded a picture and some text each time the page loaded. doing like:

document.write('<img src="'+images[randomNumber]+'" />")

etc.
What I ended up doing to make it valid was use innerHTML. I did:
function feature(){
var place = document.getElementById('featured');
place.innerHTML = '<img src="'+images[randomNumber]+'" /><p>'+words[randomNumber]+'</p>';
}

and now it comes up valid. I'm pretty sure you can use addChild in a similar manner and it would be valid as well.

No comments: