Jump to content

Smaller JavaScript File Help


The Little Guy

Recommended Posts

I have lots of AJAX functions, and all of them have the following basic lines at the beginning of each function. This uses up file space, and I was wondering if I could convert the following to a single function to include in my other AJAX functions to make my javascript file smaller. Because I would like to remove all the repetitive lines.

 

function openPOST(){
var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
var ajaxRequest;
try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			alert("Your Browser Doesn't support AJAX.");
			return false;
		}
	}
}
}

 

So then when the above is called, it will open a request thing, then I can send the information. So I could do this:

 

function someFunction(){
var someValue = openPOST();
someValue.onreadystatechange = function(){
	if(someValue.readyState == 4){
		// Do some JavaScript Stuff
	}
}
}

 

OK, so now I need to know what the first function "openPOST()" will have to return for a value.

I also need to know if my "someFunction()" is typed correctly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.