Jump to content
xisto Community
Sign in to follow this  
BlueInkAlchemist

Re-learning Javascript

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.