[web] Javascript error

Started by
1 comment, last by Phytoplankton 14 years, 11 months ago
I just know this is gonna be a stupid error, but i need to know whats up. And where else best to ask than GameDev? I have the following code:

<script type="text/&#106avascript">
function toggle(id)
{
 var thingy=document.getElementById(id);
 if (thingy.style.display=="none")
 {
  thingy.style.display = "block";
  //thingy.style.visibility="visible";
 }
 else
  {
  thingy.style.display = "none";
  //thingy.style.visibility="hidden";
 }
}

function addtask(id)
{
 var name=prompt("Please enter Task name","");
 var ble=prompt("Please enter Task Description","");
 if ((name!=null && name!="") && (ble!=null && ble!="") && (id!=null && id!=""))
   {
   window.location = "ctsk.php?action=post&pid=" + id + "&nm=" + name + "&des=" + ble;
   }  
}
   
function edittask(na,de,id,id2)
{
 var name=prompt("Please enter Task name",na);
 var ble=prompt("Please enter Task Description",de);
 if ((name!=null && name!="") && (ble!=null && ble!="") && (id!=null && id!="") && (id2!=null && id2!=""))
 {
  window.location = "ctsk.php?action=post&pid=" + id + "&nm=" + name + "&des=" + ble"&id=" + id2 ;
 }  
}

</script>
Whenever I call any of these functions, nothing happens. I ran it through a debugger and it tells me that these functions have not been defined. What can y'all see that ive done wrong? P88 - The EvilSpaceHamster
Advertisement
To really understand function scoping in &#106avascript, you need to understand that functions are first-class objects. That means that they're just pieces of code assigned to a variable. For example, the following two lines are functionally (basically) identical:

function toggle(id) {}
var toggle = function(id) {}

The first one is just syntactic sugar that makes &#106avascript look more like other common C-&#115;tyle-syntax languages.<br><br>What this means is that you need to call the functions after they have been declared. For example, the following would give you an error:<br><br>toggle('mythingy');<br><br>function toggle(id) { /* whatever */ }<br><br>This is unlike some other languages, like PHP, where a function can be called above its declaration (if they're in the same file). In &#106avascript, you need to make sure the line of code that declares the function gets run before the line of code that calls the function.<br><br>Sorry for the vague, theoretical response, but you didn't give enough code to give a specific solution. Let me know if this was helpful.
If those functions are in a separate file, did you remember to include them?
<script type="text/&#106avascript" src="MyScriptFile.js"></script>
-----OpenEndedAdventure.com - The Adventure that Anyone Can Edit.

This topic is closed to new replies.

Advertisement