/    Sign up×
Community /Pin to ProfileBookmark

Warning, loaded & long question about linking objects….viewers beware

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

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@GollumNov 04.2003 — First off, I'll add a few comments on your code as I can see a number of reasons why it doesn't work...
<i>
</i>function createDIV(newDIVName, childOfDIV)
{
if (childOfDIV = '' || childOfDIV == null) // that should be childOfDIV == ''. Single = means assignment
{
if (documents.layers &amp;&amp; window.Layer &amp;&amp; document.classes )
{
document.layers[newDIVName] = new Layer(10);
document.layers[newDIVName].visibility = 'hidden';
}
else if (document.body)
{
var tempString = '&lt;div style="position:absolute;left:0px;top:0px;width:10px;"&gt;new&lt;/div&gt;';
}

<i> </i> // shouldn't this bit be in the above else if block?
<i> </i> if (document.body.insertAdjacentHTML)
<i> </i> {
<i> </i> document.body.insertAdjacentHTML('beforeEnd', tempString);
<i> </i> }
<i> </i> else if( typeof(document.body.innerHTML) != 'undefined' ) // undefined is a JS keyword should be without quotes
<i> </i> {
<i> </i> document.body.innerHTML += tempString;
<i> </i> }
}
else
{
if (document.layers &amp;&amp; window.Layer &amp;&amp; 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 = '&lt;div style="position:absolute;left:0px;top:0px;width:10px;"&gt;new&lt;/div&gt;';
}

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


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

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

var oParent = document.body;
if ( document.getElementById &amp;&amp; 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;
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=div1&gt;Hello World&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;
&lt;a href="javascript:void 0" onclick="createDIV2('div2','div1');"&gt;Create&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ilbonparaurtiauthorNov 04.2003 — 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!!
×

Success!

Help @ilbonparaurti spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...