5,661,954 members and growing! (257 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » General     Beginner License: The Code Project Open License (CPOL)

A simple ajax class to use

By Stephen Liang

Design a simple to use ajax, for ie,firefox
Javascript, CSS, HTML (HTML), XHTML, ASP, ASP.NET, WebForms, Ajax, Design

Posted: 21 Dec 2007
Updated: 21 Dec 2007
Views: 4,663
Bookmarked: 8 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 0.53 Rating: 1.11 out of 5
2 votes, 66.7%
1
1 vote, 33.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

A simple ajax class to use. Design for ie and firefox. It just use to send data and receive it, and deal with the data yourself.

Background

I just write it suit my design.

Using the code

Now, this is the code.

Blocks of code should be set as style "Formatted" like this:

// JavaScript Document
/**
 *  Ajax Class
 *  Designer:	Stephen Liang
**/
var AjaxKtClass = function()
{
	this.xmlHttp = false;
	this.url = "";
	this.updateFun = null;

	this.Create = function(url, updateFun)
	{
		/*@cc_on @*/
		/*@if(@_jscript_version >= 5)
		try{
			this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e2){
				this.xmlHttp = false;
			}
		}
		@end @*/
		if(!this.xmlHttp&&typeof XMLHttpRequest != 'undefined'){
			this.xmlHttp = new XMLHttpRequest();
		}
		
		this.url = url;
		this.updateFun = updateFun;
	}

	this.Open = function()
	{
		this.xmlHttp.open("GET", this.url, true);
		this.xmlHttp.onreadystatechange = this.updateFun;
		this.xmlHttp.send(null);
	}
	
	this.Open2 = function(url)
	{
		this.xmlHttp.open("GET", url, true);
		this.xmlHttp.onreadystatechange = this.updateFun;
		this.xmlHttp.send(null);
	}
	
	this.Open3 = function(param)
	{
		this.xmlHttp.open("GET", this.url + param, true);
		this.xmlHttp.onreadystatechange = this.updateFun;
		this.xmlHttp.send(null);
	}
	
	this.GetData = function()
	{
		if(this.xmlHttp.readyState == 4){
			return this.xmlHttp.responseText;
		}
		return null;
	}
}
		

Use it:
<script language="javascript">
// Ajax Object
var ajaxObj = new AjaxKtClass();
ajaxObj.Create("progress.html", AjaxAct);

// Ajax act function
function AjaxAct()
{
	var ret = ajaxObj.GetData();
	if(ret == null) return;
        // Your progress here
	if(ret=="OK")
	{
	}
}

// Ajax send function
function Send()
{
	ajaxObj.Open();
//or like these
//ajaxObj.Open2("abc.html");
//ajaxObj.Open3("?abc=1");
}
</script>
		

Points of Interest

If you interest it, please emailt to me: stephen_liang@163.com

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Stephen Liang



Location: China China

Other popular Ajax and Atlas articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Search Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Dec 2007
Editor:
Copyright 2007 by Stephen Liang
Everything else Copyright © CodeProject, 1999-2008
Java | Advertise on the Code Project