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 08-26-2009, 09:03 PM   PM User | #1
IlyaZ
New to the CF scene

 
Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
IlyaZ is an unknown quantity at this point
AJAX Initialization

I don't want to create a new ajax object every time I press a button, because it's bad programming practice. (Slower + Memory Usage) (Or do I have to do that?)

So I have this init function:
Code:
function initAJAX()
{
	
	var ajaxRequest;  // magic variable
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	return ajaxRequest;
}
It returns the ajaxRequest object, or whatever it is.

In my html header source it looks like this:
Code:
<script src="ajaxinit.js">
ajaxRequest=initAJAX();
</script>
(maybe this should be in the body?)


Code:
<form name="testform">
   <input name="test_button" type="button" FISHY="ajaxFunction(ajaxRequest);" value="Test" />
   
   <input name="testbox" type="text" id="testboxid"/>
</form>
ajaxFunction handles the Ajax update of a text field displaying the time, and takes ajaxRequest as a param.

But this doesn't seem to work. Does the scope of the ajaxRequest variable end after </script>?


How can I debug the code line by line?
IlyaZ is offline   Reply With Quote
Old 08-27-2009, 07:39 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Studies have shown reusing an object versus creating a new one is no different in memory and speed.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 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 10:20 PM.


Advertisement
Log in to turn off these ads.