Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    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
    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]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •