Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2009, 02:19 PM   PM User | #1
Darklord871
New to the CF scene

 
Join Date: Apr 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Darklord871 is an unknown quantity at this point
Ajax not running Help

This is only not running in firefox works fine in internet explorer.

I am having some trouble getting some scripts to run
I am using this ajax class

Code:
// Ajax.as
// This class will make it easier to work with Ajax

function Ajax() {
	
	this.req = null;				// XMLHttpRequest object
	this.url = null;				// Location of Server Script
	this.method = "POST";			// Method of delivery
	this.async = true;				// Continue JS execution
	this.status = null;			// HTTP status code
	this.statusText = "";			// HTTP status text
	this.postData = null;			// Data to send
	this.readyState = null;		// 0:Open, 1:loading, 2:loaded, 3:downloading, 4:completed
	this.responseText = null;		// Response as a String
	this.responseXML = null;		// Response as XML document
	this.handleResp = null;		// Response handler
	this.responseFormat = "text"; 	// text, xml, object
	this.mimeType = null;			// Our MimeType
	
	
	// Init function
	// Create the XMLHttpRequest Object
	this.init = function() {
		if(!this.req) {
			try {
				// Try to create object for Firefox, Safari, IE7
				this.req = new XMLHttpRequest();
			} catch(e) {
				try {
					// Try to create object for later versions of IE
					this.req = new ActiveXObject("MSXML2.XMLHTTP");
				} catch(e) {
					try {
						// Try to create object for early versions of IE
						this.req = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
						// Could not create an XMLHttpRequest object
						return false;
					}
				}
			}
		}
		return this.req;
	};
	
	// doReq function
	// Call init, prepare request, send and error if fail 
	// Also deal with readyStates
	this.doReq = function() {
		if(!this.init()) {
			alert("ERROR: Could not create XMLHttpRequest object");
			return;
		}
		this.req.open(this.method, this.url, this.async);
		
		if(this.mimeType) {
			try {
				req.overrideMimeType(this.mimeType);
			} catch(e) {
				alert("ERROR: Couldn't override MIME type");
			}
		}
		
		var self = this; // Fix loss of scope
		this.req.onreadystatechange = function() {
			
			// Switch on current ready state
			switch(self.req.readyState) {
				case 0: // open
					// Handle open
					break;
				case 1: // loading
					// Handle loading
					break;
				case 2: // loaded
					// Handle loaded
					break;
				case 3: // downloading
					// Handle downloading
					break;
				case 4: // completed
					
					switch(self.responseFormat) {
						case "text":
							resp = self.req.responseText;
							break;
						case "xml":
							resp = self.req.responseXML;
							break;
						case "object":
							resp = req;
							break;
					}
					
					if(self.req.status >= 200 && self.req.status <=299) {
						self.handleResp(resp);
					} else {
						self.handleErr(resp);
					}
					
					break;
				default:
					alert("ERROR: Fatal readyState");
					break;
			}
		};
		
		this.req.send(this.postData);
	};
	
	// setMimeType Function
	// Set our MimeType
	this.sexMimeType = function(mimeType) {
		this.mimeType = mimeType;
	};
	
	// handleErr Function
	// Handle an error
	this.handleErr = function() {
		var errorWin;
		try {
			//errorWin = window.open("", "errorWin");
			//errorWin.document.body.innerHTML = this.responseText;
		} catch(e) {
			alert( "ERROR: An error occurred, but the error message cannot "
				  +"be displayed.\nPopup blockers must be disabled to see it.\n"
				  +"Status Code: "+this.req.status+"\n"
				  +"Status Description: "+this.req.statusText);
		}
	};
	
	// setHandlerErr Function
	// Custom error handler
	this.setHandlerErr = function(funcRef) {
		this.handleErr = funcRef;
	};
	
	// setHandlerBoth Function
	// Handle both successes and errors
	this.setHandlerBoth = function(funcRef) {
		this.handleResp = funcRef;
		this.handleErr = funcRef;
	};
	
	// abort Function
	// Stop a bad script
	this.abort = function() {
		if(this.req) {
			this.req.onreadystatechange = function() { };
			this.req.abort();
			this.req = null;
		}
	};
	
	// doGet Function
	// User function to call ajax script
	this.doGet = function(url, hand, format) {
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || "text";
		this.doReq();
	};
}
and this is the code I am trying to run


Code:
<script type="text/javascript">
	
	var start = 0;
	var maxHit = 0;
	var ajax = new Ajax();
	
	var doPoll = function() {
		start = new Date();
		start = start.getTime();
                //I am useing mod rewrite here for this link
		ajax.doGet("/process/testAjax/"+start, showPoll); 
	}
	
	window.onload = doPoll;
	
	var showPoll = function(str) {
		var pollResult = "";
		var diff = 0;
		var end = new Date();
		if(str == "ok") {
			end = end.getTime();
			diff = (end - start) / 1000;
			pollResult = diff;
		} else {
			pollResult = "error";
		}
		printResult(pollResult);
		var pollHand = setTimeout(doPoll, 100); // wait 15 seconds
	}
	
	function printResult(str) {
		/*var pollDiv = document.getElementById("pollDiv");
		if(pollDiv.firstChild) {
			pollDiv.removeChild(pollDiv.firstChild);
		}
		pollDiv.appendChild(document.createTextNode(str));*/
		
		// could just use innerHTML here
		document.getElementById('bar').style.width = (str*1000)+"px";
		document.getElementById('pollDiv').innerHTML = "Server response time: "+str+" seconds";
		
		if(str == "error"){
			document.getElementById("working").style.color = "#990000";
			document.getElementById("working").innerHTML = "Not Working";
		} else {
			document.getElementById("working").style.color = "#00cc00";
			document.getElementById("working").innerHTML = "Working";
		}
		
		if(str > maxHit) {
			maxHit = str;
			document.getElementById("max").innerHTML = "Max Delay: "+str;
		}
	}
	
</script>
<h2>Ajax Test Page <span id="working" style="color: #990000;"></span></h2>
<div id="bar" style="width: 0px; height: 10px; background-color: #009900"></div>
<div id="pollDiv"></div>
<div id="max"></div>
It comes up with a blank screen with just Ajax Test Page on it and I am not sure why =( any help would be great!

Last edited by Darklord871; 05-01-2009 at 09:35 PM..
Darklord871 is offline   Reply With Quote
Old 05-01-2009, 02:21 AM   PM User | #2
Darklord871
New to the CF scene

 
Join Date: Apr 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Darklord871 is an unknown quantity at this point
ok update - this is working on internet explorer but not firefox now.
Darklord871 is offline   Reply With Quote
Old 05-01-2009, 03:38 AM   PM User | #3
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
Surprising. Usually it's the other way 'round.

In Firefox, are there any errors in the error console? Have you installed the Firebug add-on and looked for errors there?

What is it doing / not doing that you otherwise expect?
bdl is offline   Reply With Quote
Old 05-01-2009, 04:49 PM   PM User | #4
Darklord871
New to the CF scene

 
Join Date: Apr 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Darklord871 is an unknown quantity at this point
No errors I can see =/ I have had a few people try it to make sure it wasn't just my computer, every one is able to use it on Internet explorer and not Firefox =/
Darklord871 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:07 AM.


Advertisement
Log in to turn off these ads.