Problem:
When clicking a button with an onclick attribute, the error "Uncaught ReferenceError: function is not defined" occurs.
Cause:
Userscripts run in an isolated environment where onclick operates within the target page's scope. Consequently, onclick cannot access functions defined in the userscript.
Solution:
Avoid using onclick and utilize addEventListener() instead. For instance:
emoteTab[2].innerHTML += '<span>
Updated Code:
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 += '<span>
var targetSpans = emoteTab[2].querySelectorAll ("span[data-usage]"); for (var J in targetSpans) { targetSpans[J].addEventListener ("click", appendEmote, false); }
Additional Warnings:
The above is the detailed content of Why Does \'Uncaught ReferenceError: function is not defined\' Occur with Onclick Attributes, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!