Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 12th, 2012, 06:16 PM
Authorized User
Points: 128, Level: 2
Points: 128, Level: 2 Points: 128, Level: 2 Points: 128, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
Default 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.
Reply With Quote
  #2 (permalink)  
Old January 16th, 2012, 04:16 AM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Stupid question maybe, but are you running this script within a browser? If so which one and version?
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #3 (permalink)  
Old January 16th, 2012, 09:57 AM
Authorized User
Points: 128, Level: 2
Points: 128, Level: 2 Points: 128, Level: 2 Points: 128, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
Default

I'm running it off an Internet Explorer 9 browser.
Reply With Quote
  #4 (permalink)  
Old January 16th, 2012, 10:41 AM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can you post a small example so I can try it my machine?
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #5 (permalink)  
Old January 16th, 2012, 01:05 PM
Authorized User
Points: 128, Level: 2
Points: 128, Level: 2 Points: 128, Level: 2 Points: 128, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #6 (permalink)  
Old January 17th, 2012, 04:59 AM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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?
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #7 (permalink)  
Old January 22nd, 2012, 02:09 PM
Authorized User
Points: 128, Level: 2
Points: 128, Level: 2 Points: 128, Level: 2 Points: 128, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
Default

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?
Reply With Quote
  #8 (permalink)  
Old January 25th, 2012, 04:34 AM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #9 (permalink)  
Old January 25th, 2012, 08:26 AM
Authorized User
Points: 128, Level: 2
Points: 128, Level: 2 Points: 128, Level: 2 Points: 128, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #10 (permalink)  
Old January 25th, 2012, 09:06 AM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
$ undefined error aspfun BOOK: Beginning JavaScript and CSS Development with jQuery 6 December 9th, 2009 11:20 AM
undefined offset error trev Beginning PHP 9 January 24th, 2007 04:41 AM
Undefined Index Error rit01 Beginning PHP 0 March 1st, 2006 10:03 AM
Undefined Variable: Error jjmancini Beginning PHP 2 July 27th, 2004 02:38 AM
"DbPool" undefined error. danielh Classic ASP Basics 0 June 22nd, 2003 01:09 PM



All times are GMT -4. The time now is 12:55 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.