Is it possible to achieve this task? If so, any assistance would be greatly appreciated. I am relatively new to JavaScript and have limited knowledge of syntax rules and best practices.
I've created some functions in an external file.
var myFunctions = myFunctions || {};
myFunctions.requestAjax = function () {
try{
return new XMLHttpRequest();
} catch (e) {
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser seems to have an issue! Please ensure that JavaScript functionality is enabled.");
return false;
}
}
}
}
myFunctions.onReadyStateChange = function(ajaxRequest, formName, formArray, method, controller) {
var verifiedMethod = validateMethod(method);
if (!verifiedMethod) {
throw new Error("Error: Please make sure the method passed matches 'GET' or 'POST'.");
}
for (input in formArray) {
document.formName.input.value = ajaxRequest.responseText;
}
ajaxRequest.open(method, controller, true);
ajaxRequest.send(null);
}
myFunctions.writeDropDownList = function(textFile, method, path) {
var file = myFunctions.requestAjax();
file.open(method, path, true);
if (file.readyState != 4) {
throw new Error("Error: Text file ready state is not equal to 4!");
}
if (file.status != 200) {
throw new Error("Error: Text file status is not equal to 200!");
}
var lines = file.responseText.split("\n");
document.writeln("<select>");
for(line in lines) {
document.writeln("<option>" + line + "</option>");
}
document.writeln("</select>");
file.send(null);
}
myFunctions.validateMethod = function(method) {
var lowerCaseMethod = method.toString().toLowerCase();
switch(lowerCaseMethod) {
case "get":
case "post":
return true;
default:
return false;
}
}
The HTML code trying to implement the functions
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../includes/css/adsmanager.css" />
<script type="text/javascript" src="../includes/js/myFunctions.js"></script>
</head>
<body>
<form name='adsManager'>
<button type="text" value="test" onclick="createCountriesList()">Click to create countries list</button>
</form>
</body>
</html>
I intend to use these functions for various purposes, hence the decision to create a namespace. I came across this resource which served as a reference for most of my implementation.
Appreciation to all those who can offer assistance.