 |
| Ajax the combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Ajax section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 12th, 2012, 06:16 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
|
|
window undefined error???
This question relates to a problem I get where the script tells me that there is an error that the 'window' is undefined
Here is the JavaScript code it nis a . js file:
Code:
// Stores reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject() {
var xmlHttp;
if(window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
xmlHttp = false;
}
}
else
{
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
xmlHttp = false;
}
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
function process() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
name = encodeURIComponent( document.getElementById("myName").value);
xmlHttp.open("GET", "quickstart.php?name=" + name, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}
else
setTimeout('process()', 1000);
}
function handleServerResponse() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
helloMessage = xmlDocumentElement.firstChild.data;
document.getElementById("divMessage") .innerHTML = '<i>' + helloMessage + '</i>';
setTimeout('process()', 1000);
}
else {
alert("There was a problem accessing the server: " + xmlHttp.statusText);
}
}
}
I've searvhed all over the internet but no one seems to understand why the code won't work. If anyone can help me out it'll be great.
|
|

January 16th, 2012, 04:16 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Stupid question maybe, but are you running this script within a browser? If so which one and version?
|
|

January 16th, 2012, 09:57 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
|
|
I'm running it off an Internet Explorer 9 browser.
|
|

January 16th, 2012, 10:41 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Can you post a small example so I can try it my machine?
|
|

January 16th, 2012, 01:05 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Hi, I'm not sure what you mean by example, but the script I'm writing is suppose to allow me to type a name into the text box, and have an asynchronous call to the server, come up with a response while I am still writing, as well as respond when there is nothing in the text box. The three files I'm using are all stored in my document root folder that was created when I installed Apache. (www/ajax/quickstart)
The first script is an html file called index.html
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiotional//EN"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AJAX withPHP, 2nd Edition: Quickstart</title>
<script type="text/javascript" src="quickstart.js"></script>
</head>
<body onload='process()'>
Server wants to know your name:
<input type="text" id="myName">
<div id="divMessage">
</body>
</html>
The second script is javascript file called quickstart. js
Code:
// Stores reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
// retrieves the XMLHttpRequestn object
function createXmlHttpRequestObject() {
// Stores the reference to the XMLhttpRequest object
var xmlHttp;
// if running Internet Explorer 6 or older
if(window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
xmlHttp = false;
}
}
// if running Mozilla or other browsers
else
{
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
xmlHttp = false;
}
}
// return the created object or display an error message
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
// make asynchronous HTTP request using the XMLhttpRequest object
function process() {
// proceed only if the xmlHttp object isn't busy
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
// retrieve the name typed by the user on the form
name = encodeURIComponent( document.getElementById("myName").value);
// execute the quickstart.php page from the server
xmlHttp.open("GET", "quickstart.php?name=" + name, true);
//define the method to handle server responses
xmlHttp.onreadystatechange = handleServerResponse;
// make the server request
xmlHttp.send(null);
}
else
// if the connection is busy, try again after one second
setTimeout('process()', 1000);
}
// callback function executed when a message is recieved from the server
function handleServerResponse() {
// move forward only if the transaction has completed
if (xmlHttp.readyState == 4) {
// status of 200 indicates the transaction completed successfully
if (xmlHttp.status == 200) {
// extract the XML retrieve from theserver
xmlResponse = xmlHttp.responseXML;
// obtain the document element (the root element) of the XML structure
xmlDocumentElement = xmlResponse.documentElement;
// get the text message, which is in the first child of the document element
helloMessage = xmlDocumentElement.firstChild.data;
// display the data recieved from the server
document.getElementById("divMessage") .innerHTML = '<i>' + helloMessage + '</i>';
// restart sequence
setTimeout('process()', 1000);
}
// a HTTP status different than 200 signals an error
else {
alert("There was a problem accessing the server: " + xmlHttp.statusText);
}
}
}
The 3rd script I use is a php file called quickstart.php
PHP Code:
<?php
// we'll generate XML output
header('Content-Type: text/xml');
// generate XML header
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
// create the <response>
echo '<response>';
//retrieve the user name
$name = $_GET['name'];
// generate output depending on the user name recieved from client
$usernames = array('YODA', 'AUDRA', 'BOGDAN', 'CRISTIAN');
if (in_array(strtoupper($name), $userNames))
echo 'Hello, master ' . htmlentities($name) . '!';
else if (trim($name) == '')
echo 'Stranger, please tell me your name!';
else
echo htmlentities($name) . ', I don\'t know you!';
// close the <reponse> element
echo '</response>';
?>
If you can help me figure out why it keeps showing the error 'window' is undefined, it would we appreciated
|
|

January 17th, 2012, 04:59 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Sorry, can't see why you'd get that error. Does it work in any other browser, Firefox or Chrome? Can you use a JavaScript debugger and step through the script?
|
|

January 22nd, 2012, 02:09 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
|
|
I've run it through a debugger and it doesn't bring up any errors. But it still doesn't work I haven't tried it on any other browsers. Do you have any idea what's wrong with it?
|
|

January 25th, 2012, 04:34 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Sorry, run out of ideas on this one. The page runs perfectly well on my machine (I didn't bother with the web service).
I saved the HTML and the js file to a folder and opened in IE. I set a breakpoint on the window.ActiveXObject line and also on the xmlHttp.open and xmlHttp.send ones. The first line executes as normal as does the xmlHttp.open one. Then an error occurs on xmlHttp.send as I didn't install the PHP file.
There are two possible explanations: - Your set up is radically different from mine
- Your browser is severely broken
If the later then I'm surprised that any web pages work, the window object is used implicitly or explicitly in virtually every piece of JavaScript executed in the browser.
|
|

January 25th, 2012, 08:26 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Well all the websites I been on seem to work okay, so I'm guessing the problem is with my set up. I'm not sure sure what you mean by setup, but could you please explain what I would need to look at or change in my set up for this code to work.
Thanks
|
|

January 25th, 2012, 09:06 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
What I meant was that all three files are in a folder on your web server and you are using Internet Explorer to browse to index.html via a URL such as: http://myServer/mySite/index.html
If that's the case then I can't think why the window object is not recognised.
|
|
 |