SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cant find problem in code

    Ive been skimming this code for about 20 minutes now and i cant find the reason why this code isnt working. Its not retrieving the file blah.html (wihch is in the same directory as this file im showing you. the file contains this:

    hahhaah

    I am supposed to be able to click the link then on a new line display those words. Here is the code

    Code:
    <html>
    <head>
    <title>Hello</title>
    <script language="javascript" type="text/javascript">
    function getXmlHttpRequestObject() {
    	if (window.XMLHttpRequest) {
    		return new XMLHttpRequest();
    		}
    	else if (window.ActiveXObject) {
    		return new ActiveXObject("Microsoft.XMLHTTP");
    		}
    	else {
    		alert("Your browser doesn't support the XMLHttpRequest. To fix this error please download FireFox 3");
    	}
    }
    var recieveReq = getXmlHttpRequestObject();
    
    function sayHello() {
    	if (recieveReq.readyState==4 || recieveReq.readyState==0) {
    		recieveReq.open("GET", 'blah.html', true);
    		recieveReq.onreadystatechange = handleSayHello;
    		recieveReq.send(null);
    	}
    }
    
    function handleSayHello();
    {
    if(recieveReq.readyState == 4) {
    	document.getElementById('span_result').innerHTML = recieveReq.responseText;
    }
    }
    </script>
    </head>
    <body>
    <a href="javascript:sayHello()">Hello</a><br />
    <span id="span_result"></span>
    </body>
    </html>
    and Ive been copying this code in an attempt to memorize the function and help myself learn. i cant find the differences in this code to the code right here..:

    Code:
    <html>
    	<head>
    		<title>The Hello World of AJAX</title>
    		<script language="JavaScript" type="text/javascript">
    			//Gets the browser specific XmlHttpRequest Object
    			function getXmlHttpRequestObject() {
    				if (window.XMLHttpRequest) {
    					return new XMLHttpRequest(); //Not IE
    				} else if(window.ActiveXObject) {
    					return new ActiveXObject("Microsoft.XMLHTTP"); //IE
    				} else {
    					alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
    				}
    			}			
    			//Get our browser specific XmlHttpRequest object.
    			var receiveReq = getXmlHttpRequestObject();		
    			function sayHello() {
    				//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
    				if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
    					receiveReq.open("GET", 'SayHello.html', true);
    					//Set the function that will be called when the XmlHttpRequest objects state changes.
    					receiveReq.onreadystatechange = handleSayHello;
    					receiveReq.send(null);
    				}			
    			}
    			//Called every time our XmlHttpRequest objects state changes.
    			function handleSayHello() {
    				//Check to see if the XmlHttpRequests state is finished.
    				if (receiveReq.readyState == 4) {
    					//Set the contents of our span element to the result of the asyncronous call.
    					document.getElementById('span_result').innerHTML = receiveReq.responseText;
    				}
    			}
    			</script>
    	</head>
    	<body>
    		<!-- Clicking this link initiates the asyncronous request -->
    		<a href="javascript:sayHello();">Say Hello</a><br />
    		<!-- used to display the results of the asyncronous request -->
    		<span id="span_result"></span>
    	</body>
    </html>
    Please helpppp

  2. #2
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yay i figured it out. I had a ; next to a function *doh. also I had <a href="javascript:sayHello" without the ; at the end of that. Woo hooo my first program in ajax. Wow that is a lot more complicated then c++ or php lol


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •