[web] Javascript error

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

{
if ((name!=null && name!="") && (ble!=null && ble!="") && (id!=null && id!=""))
{
window.location = "ctsk.php?action=post&pid=" + id + "&nm=" + name + "&des=" + ble;
}
}

{
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 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 on other sites
If those functions are in a separate file, did you remember to include them?
<script type="text/javascript" src="MyScriptFile.js"></script>

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627676
• Total Posts
2978582

• 11
• 12
• 10
• 12
• 22