I'm trying to make a userscript for a website to add custom emotes. However, I've been getting a lot of errors.
Here is the function:
function saveEmotes[] {
removeLineBreaks[];
EmoteNameLines = EmoteName.value.split["\n"];
EmoteURLLines = EmoteURL.value.split["\n"];
EmoteUsageLines = EmoteUsage.value.split["\n"];
if [EmoteNameLines.length == EmoteURLLines.length && EmoteURLLines.length == EmoteUsageLines.length] {
for [i = 0; i < EmoteURLLines.length; i++] {
if [checkIMG[EmoteURLLines[i]]] {
localStorage.setItem["nameEmotes", JSON.stringify[EmoteNameLines]];
localStorage.setItem["urlEmotes", JSON.stringify[EmoteURLLines]];
localStorage.setItem["usageEmotes", JSON.stringify[EmoteUsageLines]];
if [i == 0] {
console.log[resetSlot[]];
}
emoteTab[2].innerHTML += '';
} else {
alert["The maximum emote[" + EmoteNameLines[i] + "] size is [36x36]"];
}
}
} else {
alert["You have an unbalanced amount of emote parameters."];
}
}
The span
tag's title
calls this function:
function appendEmote[em] {
shoutdata.value += em;
}
Every time I click a button that has an title
attribute, I get this error:
Uncaught ReferenceError: function is not defined.
Update
I tried using:
emoteTab[2].innerHTML += '';
document.getElementById[EmoteNameLines[i]].addEventListener["click", appendEmote[EmoteUsageLines[i]], false];
But I got an undefined
error.
Here is the script.
I tried doing this to test if listeners work and they don't for me:
emoteTab[2].innerHTML = 'Custom IconsSmilies
Popup ';
document.getElementById["togglemenu"].addEventListener["click", changedisplay,false];
Sometimes, you may come across JavaScript function is not defined error that looks like the following:
Uncaught ReferenceError: doAction is not defined
The ReferenceError
as in the case above is caused when you call something that’s not defined in JavaScript. Let me show you several things you can do to fix the error.
Make sure the function is defined inside your script
One of the small mistakes that could cause the error is that you haven’t
defined the function properly. You need to define the function using either the function
keyword:
function doAction[] {
// your function code here
}
Or using the arrow function syntax as follows:
const doAction = [] => {
// your function code here
};
Please keep in mind that functions defined through function expressions must be defined before the call. Function expressions are functions that you defined through a variable keyword as follows:
var fnAction = function [] {};
// or
let fnAction = [] => {};
From the example code above, the variable fnAction
will be hoisted, but the function declaration is not, so it will be undefined as shown below:
fnAction[]; // fnAction is not defined
let fnAction = [] => {
console.log["Executing action"];
};
See also: JavaScript hoisting behavior
That’s why it’s always better to define the function before calling it.
When you have defined the function, try calling it immediately below the declaration to see if it works:
function doAction[] {
// your function code here
}
doAction[]; // test the function call
If it’s running without any error, then you may have several lines of code after the declaration that causes the script to malfunction.
Make sure the entire script has no error
If you’re putting the function into a script and calling it from an HTML tag, you need to make sure that the entire script has no error or the function won’t be loaded.
For example, notice how there is an extra ]
right next to getElementById
call:
Function not defined
Click me
document.getElementById["action"]]; // extra ']' here
function fnAction[]{
console.log["Executing action"];
}
Although there’s no error on the fnAction[]
code, an error in any part of the script will cause the browser to ignore the rest of that script. You need to fix the error first so the rest of the code can be executed by the browser.
One way to see if you have any error is to run the HTML page and check on the console as follows:
You may find the ReferenceError
fixed itself as you fix JavaScript errors from your scripts.
Make sure the script is loaded before the call.
Finally, the function is not defined error can also be caused by calling the function before the script is loaded to the browser. Suppose you have a JavaScript file separated from your HTML file as follows:
// script.js
function fnAction[] {
console.log["executing action"];
}
Then you load the script into your
HTML file, but you call the fnAction
function before you load the script as follows:
Load script demo
fnAction[];
// Uncaught ReferenceError: fnAction is not defined
The same also happens when you call it on the tag:
Function not defined
fnAction[];
To fix this, you need to call the function below the call:
Function not defined
fnAction[];
Those are the three ways you can try to fix function is not defined error in JavaScript.