View Full Version : Can someone get this to validate?

02-22-2007, 09:23 PM
Hi Gang,
I've been getting my web site code validated, and I have run across a little problem:

document.write('<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>')

The above code is throwing an error when I try to validate. It keeps complaining about the three </div> tags in this string. I think there is a way to wrap this code so that this will validate, but I'm not sure how to do it. Can someone help me out?
Thanks in advance,

02-22-2007, 09:29 PM
its usually because of the </ in close proximity..try this
'<' + '/' + 'div>'

02-22-2007, 09:42 PM
This is described in Section B.3.2.1 (http://www.w3.org/TR/html401/appendix/notes.html#h-B.3.2.1) of the HTML 4.01 Specification. It also shows an easier way to avoid that character combination (<\/).

02-23-2007, 04:20 AM
Hey Arbitrator & Shyam,
thanks for the quick replies. Shyam, your solution worked. I do understand what Arbitrator is saying about there being an easier way.... I saw something on using the '\' to fix this, but when I tried it the script didn't run. I don't know if I did something wrong or not....
Arbitrator, could you please change this string of code so that I know it is correct and then I can see if I did something wrong?
Thanks again,

02-23-2007, 04:50 AM
You just stick the symbol into the middle of every </ combination. It’s effectively escaping the forward slash (doing nothing), to keep the two characters apart.

Here’s a functional example. It uses a three-div structure as seen in your own code.

I didn’t use your exact code, however, because it references things I don’t have access to and makes use of bad practice (inline CSS). document.write() itself is also bad practice, but for the sake of demonstration, I left it alone.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"

<html lang="en-US">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>HTML 4.01 Strict Document</title>

<meta name="Author" content="Patrick Garies">

<style type="text/css">
* { margin: 0; }
html { padding: 1em; }
*#container {
border: 1px solid;
*#container div {
margin: 1em;
border: 1px solid #ccc;
background-color: #eee;
*#container div + div {
font-variant: small-caps;
span.date { font-weight: bold; }


<script type="text/javascript">
var date = new Date();
var days = [
var day = days[date.getDay()];
document.write("<div id='container'>\
<div>Today is <span class='date'>" + day + "<\/span>.<\/div>\
<div>It is day " + date.getDate() + " of this month.<\/div>\


02-25-2007, 02:31 AM
Ok Arbitrator,
Thanks. I got this script off of the net, not really the best web coder. How would you go about replacing document.write? I think I see a re-write of this script coming, maybe a chance to learn something while I'm at it!

02-25-2007, 06:41 AM
How would you go about replacing document.write?An example is below; this uses basic DOM methods (http://www.quirksmode.org/dom/w3c_core.html). This would go in the head of the document. All of the CSS would be assigned through classes or IDs in style sheets. The insertion of the content would be done during the load of the document using events like load or DOMContentLoaded.

This is much more verbose, so some people use Microsoft’s shortcut of innerHTML. You should at least know how to do things this way though since not everyone’s going to support Microsoft’s extensions. You should also know it since document.write() doesn’t work in XML, including XML languages such as XHTML.

var div1 = document.createElement("div");
var div2 = document.createElement("div");
var div3 = document.createElement("div");
div1.id = divID;
div1.className = yourClass1;
div2.id = divID + "1";
div2.className = div3.className = innerDiv;
div3.id = divID + "2";
document.body.appendChild(div1);Blue variables reference content not shown here.

02-25-2007, 11:33 PM
Ok Arbitrator,
I think I get it...
I will try to put this to use for this page I am trying to validate, and see if I can get it to work. I will let you know how I make out! (Or I'll be bakc crying for help!;) )