473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Javascript code not working

Can you tell me , why this code does not run:

<%@ Page Language="C#"
MasterPageFile="~/MasterPage.master"
AutoEventWireup="true"
CodeFile="AJAX6.aspx.cs"
Inherits="Experiments_AJAX_AJAX6"
Title="XML and SQL" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"
Runat="Server">
<script type="text/javascript">
var xmlHttp;

function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function doSearch() {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "/App_Data/books.xml", true);
xmlHttp.send(null);
}
function startRequest(requestedList) {
requestType = requestedList;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
//alert(xmlHttp.onreadystatechange);
xmlHttp.open("GET", "/App_Data/books.xml", true);
xmlHttp.send(null);
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
clearPreviousResults();
if( requestType == "author" )
booksByAuthor();
else
listAllBooks();
}
}
}

function clearPreviousResults() {
var header = document.getElementById("header");
if(header.hasChildNodes()) {
header.removeChild(header.childNodes[0]);
}

var tableBody = document.getElementById("resultsBody");
while(tableBody.childNodes.length 0) {
tableBody.removeChild(tableBody.childNodes[0]);
}
}

function listAllBooks() {
var results = xmlHttp.responseXML;

var book = null;
var isbn = "";
var year = "";
var title = "";
var author = "";
var book_author = "";
var keyword = "";
var book_keyword = "abc";

addTableRow("ISBN", "TITLE" , "AUTHOR","YEAR", "KEYWORD");
var book = results.getElementsByTagName("book");
for(var i = 0; i < book.length; i++) {
isbn = book[i].getElementsByTagName("isbn")
[0].firstChild.nodeValue;
year = book[i].getElementsByTagName("year")
[0].firstChild.nodeValue;
title = book[i].getElementsByTagName("title")
[0].firstChild.nodeValue;

keyword = "";
book_keyword = book[i].getElementsByTagName("b_keywords");

for ( var j = 0; j < book_keyword[0].childNodes.length ; j++ )
{
if (keyword)
keyword = keyword+ ", " +
book_keyword[0].getElementsByTagName("keyword")
[j].firstChild.nodeValue;
else
keyword = book_keyword[0].getElementsByTagName("keyword")
[j].firstChild.nodeValue;
}
author = "";
book_author = book[i].getElementsByTagName("b_authors");
for ( var j = 0; j < book_author[0].childNodes.length ; j++ )
{
if ( author )
author = author+ ", " +
book_author[0].getElementsByTagName("author")[j].firstChild.nodeValue;
else
author = book_author[0].getElementsByTagName("author")
[j].firstChild.nodeValue;
}

addTableRow(isbn, title, author, year,keyword);
}

var header = document.createElement("h3");
var headerText = document.createTextNode("Results:");
header.appendChild(headerText);
document.getElementById("header").appendChild(head er);

document.getElementById("resultsTable").setAttribu te("border",
"1");
}
function booksByAuthor() {
var results = xmlHttp.responseXML;

var book = null;
var isbn = "";
var year = "";
var title = "";
var author = "";
var book_author = "";
var keyword = "";
var book_keyword = "abc";
var authorName = "";

addTableRow("ISBN", "TITLE" , "AUTHOR","YEAR", "KEYWORD");
var book = results.getElementsByTagName("book");
for(var i = 0; i < book.length; i++) {
isbn = book[i].getElementsByTagName("isbn")
[0].firstChild.nodeValue;
year = book[i].getElementsByTagName("year")
[0].firstChild.nodeValue;
title = book[i].getElementsByTagName("title")
[0].firstChild.nodeValue;

keyword = "";
book_keyword = book[i].getElementsByTagName("b_keywords");

for ( var j = 0; j < book_keyword[0].childNodes.length ; j++ )
{
if (keyword)
keyword = keyword+ ", " +
book_keyword[0].getElementsByTagName("keyword")
[j].firstChild.nodeValue;
else
keyword = book_keyword[0].getElementsByTagName("keyword")
[j].firstChild.nodeValue;
}
author = "";
book_author = book[i].getElementsByTagName("b_authors");
for ( var j = 0; j < book_author[0].childNodes.length ; j++ )
{
if ( author )
author = author+ ", " +
book_author[0].getElementsByTagName("author")[j].firstChild.nodeValue;
else
author = book_author[0].getElementsByTagName("author")
[j].firstChild.nodeValue;
}

authorName = document.getElementById("authorName") ;
for ( j = 0; j < authorName.options.length ; j++ )
if ( authorName.options[j].selected )
if( author.indexOf( authorName.options[j].value ) !=
-1 )
addTableRow(isbn, title, author, year,keyword);
}

var header = document.createElement("h3");
var headerText = document.createTextNode("Results:");
header.appendChild(headerText);
document.getElementById("header").appendChild(head er);

document.getElementById("resultsTable").setAttribu te("border",
"1");
}

function addTableRow(isbn, title, authors, year,keywords) {
var row = document.createElement("tr");

var cell = createCellWithText(isbn);
row.appendChild(cell);

cell = createCellWithText(title);
row.appendChild(cell);

cell = createCellWithText(authors);
row.appendChild(cell);

cell = createCellWithText(year);
row.appendChild(cell);

cell = createCellWithText(keywords);
row.appendChild(cell);

document.getElementById("resultsBody").appendChild (row);
}
function addTableHeading(isbn, title, authors, year,keywords) {
var thElement = document.createElement("THEAD");

var trElement = document.createElement("tr");
thElement.appendChild(trElement);

var row = document.createElement("th");
trElement.appendChild(row);

var cell = createCellWithText(isbn);
row.appendChild(cell);

var row = document.createElement("th");
trElement.appendChild(row);

cell = createCellWithText(title);
row.appendChild(cell);

var row = document.createElement("th");
trElement.appendChild(row);

cell = createCellWithText(authors);
row.appendChild(cell);

var row = document.createElement("th");
trElement.appendChild(row);

cell = createCellWithText(year);
row.appendChild(cell);

var row = document.createElement("th");
trElement.appendChild(row);

cell = createCellWithText(keywords);
row.appendChild(cell);

document.getElementById("resultsBody").appendChild (row);
}

function createCellWithText(text) {
var cell = document.createElement("td");
var textNode = document.createTextNode(text);
cell.appendChild(textNode);

return cell;
}
</script>


<center>
<h4 align = "center"><font color = "#000" size = "5">Search
Books !!</font</h4>
<br/>
<table>
<tr>
<td><input type="button" value="All Books"
onclick="startRequest('all')"/></td>
</tr>
<tr>
<td>&nbsp; &nbsp; &nbsp; &nbsp; List books by: &nbsp; &nbsp;
&nbsp; &nbsp;</td>
</tr>
<tr><td>
<select id="authorName">
<option value="" >Select Author</option>
<option value="Harvey M. Deitel">Harvey M. Deitel</option>
<option value="Stephen King">Stephen King</option>
<option value="Paul J. Deitel">Paul J. Deitel</option>
<option value="Larry Wall">Larry Wall</option>
<option value="Jon Orwant">Jon Orwant</option>
<option value="Adam W. Chase">Adam W. Chase</option>
<option value="Ed Kugler">Ed Kugler</option>
<option value="Eric Harr">Eric Harr</option>
<option value="Charles W. Henderson">Charles W.
Henderson</option>

</select>

<input type="button" value="Search"
onclick="startRequest('author')"/></td>
</tr>
</table>

<span id="header">

</span>

<table id="resultsTable" width="75%" >
<tbody id="resultsBody">
</tbody>
</table>
</center>
</asp:Content>

The XML file is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<myBooks>
<book>
<isbn>0743289412</isbn>
<title>Lisey's Story</title>
<b_authors>
<author>Stephen King</author>
</b_authors>
<year>2006</year>
<b_keywords>
<keyword>Novel</keyword>
</b_keywords>
</book>

<book>
<isbn>0451211243</isbn>
<title>The Dark Tower Box Set</title>
<b_authors>
<author>Stephen King</author>
</b_authors>
<year>2003</year>
<b_keywords>
<keyword>Novel</keyword>
</b_keywords>
</book>
<book>
<isbn>0131483986</isbn>
<title>Java: How to Program</title>
<b_authors>
<author>Harvey M. Deitel</author>
<author>Paul J. Deitel</author>
</b_authors>
<year>2004</year>
<b_keywords>
<keyword>Java</keyword>
</b_keywords>
</book>

<book>
<isbn>0596000278</isbn>
<title>Programming Perl</title>
<b_authors>
<author>Larry Wall</author>
<author>Jon Orwant</author>
</b_authors>
<year>2000</year>
<b_keywords>
<keyword>Perl</keyword>
</b_keywords>
</book>
<book>
<isbn>1579547486</isbn>
<title>Triathlon Training for the Rest of Us</title>
<b_authors>
<author>Eric Harr</author>
</b_authors>
<year>2003</year>
<b_keywords>
<keyword>Triathlon</keyword>
</b_keywords>
</book>

<book>
<isbn>0425103552</isbn>
<title>Marine Sniper</title>
<b_authors>
<author>Charles W. Henderson</author>
</b_authors>
<year>1988</year>
<b_keywords>
<keyword>sniper</keyword>
</b_keywords>
</book>
</myBooks>

Apr 12 '07 #1
0 1287

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
1
by: Muhammad Abdullah | last post by:
Hi am having some problems with the javascript confirm. i have it working fine on one page and it doesnt even pop up at the other. The code on the working page is, private void...
1
by: den2005 | last post by:
Hi everybody, I am confused and still looking why this codes is not working. Can anyone notice or know why this code is not working? Thanks in advance. Code working: <form id="form1"...
11
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.