Well, 2 reasons:
- 1. Problems with
WarnUser
WarnUser has 2 lines, where each has a problem. The first line is:
var reason = prompt("What are you warning for?").log('#WallMessageTitle');
To the browser, this line takes the user input from the prompt window and executes its "log" method. If the user did submit some text, an error would occur, since strings don't have a "log" method (unless your wiki has set a String.prototype.log
method). If the user presses "cancel" in the popup, that's even worse, since the returend value would be null
, and attempting to set or get properties from null always throws an error. Same story with the second line. "log" isn't a method of strings.
- 2. Problems with creating the markup
document.createElement
is meant to be passed a single argument- the element's tag name- it cannot be used for parsing HTML. If you want to parse some HTML, here are your options:
/* suppose you've declared 'WarnUser' above already */
// option 1: use jQuery
$('<span class="wikia-button" onclick="WarnUser()">Warn this user</span>').prependTo('.tabs');
// ###########################################
// option 2: use native javascript
var span = document.createElement("span"),
btn;
span.className = "wikia-button";
span.textContent = "Warn this user";
Array.prototype.forEach.call(document.querySelectorAll(".tabs"), function(tab) {
btn = span.cloneNode(true);
btn.addEventListener("click", WarnUser);
tab.insertBefore(btn, tab.childNodes[0]);
});
// ###########################################
// option 3: use DOMParser
var btn = new DOMParser().parseFromString('<span class="wikia-button" onclick="WarnUser()">Warn this user</span>', "text/html");
Array.prototype.forEach.call(document.querySelectorAll(".tabs"), function(tab) {
tab.insertBefore(btn.querySelector("span"), tab.childNodes[0]);
});