I have a small text censor web app im working on. Whenever the .js is in its own file and I click the 'censor text' button, I get a HTTP error 405, but if i put the js in the HTML tag, it works fine.Not too sure why.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document Censor</title>
<script src="/js/main.js"></script>
</head>
<body>
<h1>Text Censor</h1>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Rerum enim reiciendis recusandae aliquid delectus
consectetur nobis. Sunt exercitationem iure dolores provident obcaecati aliquam totam. Molestiae cumque ducimus
aliquid. Deserunt, provident.</p>
<form name="redacted" method="post" action="">
<textarea id="input" name="text" rows="10" cols="60">
</textarea>
<br />
<input id="formSub" type="submit" value="Censor Text" />
</form>
</body>
</html>
JS:
var div = document.getElementById('formSub');
function censorWords(event) {
event.preventDefault();
var textContent = document.getElementById('input');
var redacted = ["boston", "red", "sox"];
console.log(textContent.value)
textContent.value = censored(textContent.value, redacted);
}
function censored(string, filters) {
console.log('in')
// "i" ignores case, "g" for global and "|" for OR match
var regexp = new RegExp(filters.join("|"), "gi");
return string.replace(regexp, function (match) {
//this is where the words are replaced with X
var censorship = '';
for (var i = 0; i < match.length; i++) {
censorship += 'x';
}
return censorship
})
}
div.addEventListener('click', censorWords)