Honesty Rocks! truth rules.

Re-learning Javascript

HOME      >>       Programming

BlueInkAlchemist

As rusty as I was with ActionScript when I started my new job, I'm even rustier at JavaScript. I'm trying to generate a new div on a webpage, containing an html file that (at the moment) holds only a simple .gif image. The page looks like this at the moment:

<html><head><script language="text/javascript" language="Javascript"> <!-- Hide script from older browsers function creatediv(id, html, width, height, left, top) { var newdiv = document.createElement('div'); newdiv.setAttribute('id', id); if (width) { newdiv.style.width = 300; } if (height) { newdiv.style.height = 250; } if ((left || top) || (left && top)) { newdiv.style.position = "absolute"; if (left) { newdiv.style.left = left; } if (top) { newdiv.style.top = top; } } newdiv.style.background = "#00C"; newdiv.style.border = "4px solid #000"; if (html) { newdiv.innerHTML = html; } else { newdiv.innerHTML = "nothing"; } document.body.appendChild(newdiv); } // End hiding script from older browsers --></script><title>JavaScript Test</title></head><body><a href="java script:creatediv(test,test.html,300,250)">Test Link</a></body></html>

Can you folks take a look at my code and let me know what, if anything, I'm doing wrong? Right now all the test link does is look at me funny when I click on it.


truefusion

Your problem is not in the JavaScript itself but in the HTML:

<script language="text/javascript" language="Javascript">

You have the attribute language defined twice. The first instance should be "type" instead of "language" (but you can do without the language attribute):

<script type="text/javascript">

Now the next problem you'd have would be that the variable test is not defined:

<a href="java script:creatediv(test,test.html,300,250)">Test Link</a>


BlueInkAlchemist

Now the next problem you'd have would be that the variable test is not defined:

<a href="java script:creatediv(test,test.html,300,250)">Test Link</a>

Since the function has 'id' as it's first variable, wouldn't 'test' be passed to the javascript function as the value for that variable?

 

<a href="#" onclick="creatediv('test','test.html','300','250'); return false;">Test Link</a>


truefusion

Since the function has 'id' as it's first variable, wouldn't 'test' be passed to the javascript function as the value for that variable?

<a href="#" onclick="creatediv('test','test.html','300','250'); return false;">Test Link</a>

It will now in this code since you added the single quotes. But you don't need the single quotes for the numbers; they are taken as integers and get parsed properly without the single quotes.