onclick = functionでボタンタイプを使用して、htmlから呼び出そうとするこのjavascriptコードを取得しました。残念ながら、エラーが表示されます: function "loadData()" is undefined で、その理由がわかりません。これは私のJavaScriptコードです:
function loadData(url) {
var serialNumber = document.getElementById('searchBox').value;
var xmlhttp;
var txt, xx, x, i;
alert(serialNumber);
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
txt = "<table border='1'><tr><th>Serial</th><th>Product number</th><th>Product name</th><th>Account name</th><th>Document number</th></tr>";
x = xmlhttp.responseXML.documentElement.getElementsByTagName("PRODUCT");
for (i = 0; i < x.length; i++) {
if (
txt = txt + "<tr>";
xx = x[i].getElementsByTagName("SERIAL");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </td>";
}
}
xx = x[i].getElementsByTagName("PRODNM");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </td>";
}
}
xx = x[i].getElementsByTagName("PRODNR");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </td>";
}
}
xx = x[i].getElementsByTagName("ACCNAME");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </td>";
}
}
xx = x[i].getElementsByTagName("NRDOC");
{
try {
txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er) {
txt = txt + "<td> </td>";
}
}
txt = txt + "</tr>";
}
txt = txt + "</table>";
document.getElementById('showTable').innerHTML = txt;
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
これは、その関数を呼び出す HTML コードです。
<input type="text" id="searchBox" value="Enter serial number here" onFocus="this.value=''"/>
<input type="button" id="search" value="Search" onclick="loadData('prod_catalog.xml')"/>