Sign in to follow this  

[web] Javascript error

This topic is 3146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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/javascript">
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

Share this post


Link to post
Share on other sites
To really understand function scoping in javascript, 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 javascript look more like other common C-style-syntax languages.

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:

toggle('mythingy');

function toggle(id) { /* whatever */ }

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 javascript, you need to make sure the line of code that declares the function gets run before the line of code that calls the function.

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.

Share this post


Link to post
Share on other sites

This topic is 3146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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