Click to See Complete Forum and Search --> : Warning, loaded & long question about linking objects....viewers beware


Dudsmack
11-04-2003, 12:10 AM
I feel dirty posting this up here but I've been racking my brain on this one for some two days now and I'm stuck.

I'm writing a function to create a DIV object on the page. My idea is that'll it work on all 4th+ generation browsers. FOR THE LIFE OF ME I CAN'T GET IT TO WORK!!!

fnc has two arguments, 'newDIVName', which is suppose to be the name of the new DIV and 'childOfDIV', which is suppose to allow me to make the new div a child of an existing div. here's my code:


function createDIV(newDIVName, childOfDIV)
{
if (childOfDIV = '' || childOfDIV == null)
{
if (documents.layers && window.Layer && document.classes )
{
document.layers[newDIVName] = new Layer(10);
document.layers[newDIVName].visibility = 'hidden';
}
else if (document.body)
{ var tempString = '<div style="position:absolute;left:0px;top:0px;width:10px;">new</div>'; }
if (document.body.insertAdjacentHTML)
{ document.body.insertAdjacentHTML('beforeEnd', tempString); }
else if( typeof(document.body.innerHTML) != 'undefined' )
{ document.body.innerHTML += tempString; }
}
else
{
if (document.layers && window.Layer && document.classes )
{ document.layers[newDIVName] = new Layer(10, newDIVName);
document.layers[newDIVName].visibility = 'hidden'; }
else if (document.body)
{ var tempString = '<div style="position:absolute;left:0px;top:0px;width:10px;">new</div>'; }
if (newDIVName.insertAdjacentHTML)
{ (eval(newDIVName)).insertAdjacentHTML('beforeEnd', tempString); }
else if( typeof(document.body.innerHTML) != 'undefined' )
{ newDIVName.innerHTML += tempString; }
}
}

Gollum
11-04-2003, 02:45 AM
First off, I'll add a few comments on your code as I can see a number of reasons why it doesn't work...

function createDIV(newDIVName, childOfDIV)
{
if (childOfDIV = '' || childOfDIV == null) // that should be childOfDIV == ''. Single = means assignment
{
if (documents.layers && window.Layer && document.classes )
{
document.layers[newDIVName] = new Layer(10);
document.layers[newDIVName].visibility = 'hidden';
}
else if (document.body)
{
var tempString = '<div style="position:absolute;left:0px;top:0px;width:10px;">new</div>';
}

// shouldn't this bit be in the above else if block?
if (document.body.insertAdjacentHTML)
{
document.body.insertAdjacentHTML('beforeEnd', tempString);
}
else if( typeof(document.body.innerHTML) != 'undefined' ) // undefined is a JS keyword should be without quotes
{
document.body.innerHTML += tempString;
}
}
else
{
if (document.layers && window.Layer && document.classes )
{
document.layers[newDIVName] = new Layer(10, newDIVName);
document.layers[newDIVName].visibility = 'hidden';
}
else if (document.body)
{
// tempString is already defined above. JS doesn't work like C++, vars are defined function-wide
var tempString = '<div style="position:absolute;left:0px;top:0px;width:10px;">new</div>';
}

// in the above else if block?
if (newDIVName.insertAdjacentHTML)
{
// doesn't make sense - newDIVName is the div you are creating!!!
(eval(newDIVName)).insertAdjacentHTML('beforeEnd', tempString);
}
else if( typeof(document.body.innerHTML) != 'undefined' )
{
newDIVName.innerHTML += tempString;
}
}
}


Now, the way I would do this is...


<html>
<head>
<script language="javascript">
function createDIV2(divID, parentDIV)
{
var tmp = '<div id="' + divID + '" style="position:absolute;left:0px;top:0px;width:10px;">new</div>';

var oParent = document.body;
if ( document.getElementById && parentDIV != undefined ) oParent = document.getElementById(parentDIV);

if ( document.layers ) // NS4
{
var lParent = (parentDIV != undefined) ? document.layers[parentDIV] : null;
document.layers[divID] = (lParent != null) ? new Layer(10,lParent) : new Layer(10);
document.layers[divID].hidden = false;
document.layers[divID].visibility = "show";
document.layers[divID].document.open();
document.layers[divID].document.write('new');
document.layers[divID].document.close();
}
else if ( document.body.insertAdjacentHTML ) // for IE4+
{
oParent.insertAdjacentHTML('BeforeEnd', tmp);
}
else if ( document.body.innerHTML != undefined )
{
oParent.innerHTML += tmp;
}
}
</script>
</head>
<body>
<div id=div1>Hello World</div>
<br>
<br>
<a href="javascript:void 0" onclick="createDIV2('div2','div1');">Create</a>
</body>
</html>

Dudsmack
11-04-2003, 06:28 AM
Thanks big time! I feel stupid for not using the == (the first problem you pointed out) but as far as the rest your help I really appreciate it. Thanks man! It's good folk like you that make the Internet such a great resource!!


Now I just need to get out of the C++ mindset...rock on!!