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
https://forums.phpfreaks.com/topic/117714-smaller-javascript-file-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.