Focus in Textbox when the page is Loaded through Ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • me2tech
    New Member
    • Feb 2007
    • 14

    Focus in Textbox when the page is Loaded through Ajax

    Hi,

    This is ram.

    I am developing a page(usereg.jsp ) with two textboxes one for username and another for email. Since i am using this page in main.jsp using Ajax, I am removing the body part in usereg.jsp.

    Now i need to focus the cursor in first textbox of usereg.jsp. Since there is no body tag in this jsp i cannot call onload="documen t.formname.text boxname.focus() ".

    But i need the focus in textbox when i call this page in main.jsp.

    Even i have tried to focus the texbox in usereg.jsp by writing the seperate java script in this page. When i execute just this page(usereg.jsp ) the problem of focusing text box is solved,,

    But when i call this page in main.jsp along with the java script i wrote, the focus is not reflecting.

    Please suggest me where am i going wrong..

    my mailid is: ****
    Last edited by acoder; Feb 26 '07, 07:11 PM. Reason: email removed
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    If you are using Ajax, use the onreadystatecha nge event handler. Once the value is 4, you can call a function which deals with the response so include the focus code in that function.

    Comment

    • webhead
      New Member
      • Mar 2006
      • 56

      #3
      I'm having the same problem but tried doing the focus in the function and it still doesn't work. Here's my javascript:
      Code:
      function doStuff() {
      	recn=document.getElementById('recno').value;
      	str = "moreStuff.php?recn="+recn;
      	http.open("GET", str, true);
      	http.onreadystatechange = handleHttpResponse;
      	http.send(null);
      	recno.focus();
      	}
      Is this the wrong place to call for focus, or do I have the call syntax wrong?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by webhead
        Is this the wrong place to call for focus, or do I have the call syntax wrong?
        The first 'A' in AJAX stands for 'Asynchronous'. In other words, handleHttpRespo nse won't get executed until your AJAX request returns. However, your script will continue to evaluate code until the request returns.

        So when you put recno.focus() inside of doStuff:

        Originally posted by webhead
        Code:
        	http.send(null);
        	recno.focus();
        	}
        recno.focus() gets evaluated immediately after http.send gets executed, regardless of whether or (more likely) not the http request has returned.

        What you want to do instead is to include recno.focus() inside of handleHttpRespo nse, so that it doesn't get called until your AJAX call returns.

        [EDIT:
        E.g.:

        Code:
        function handleHttpResponse(response) {
        	.
        	.
        	.
        	//	recno is a global variable, right?
        	recno.focus();
        }
        ]

        Comment

        • webhead
          New Member
          • Mar 2006
          • 56

          #5
          Originally posted by pbmods
          The first 'A' in AJAX stands for 'Asynchronous'. In other words, handleHttpRespo nse won't get executed until your AJAX request returns. However, your script will continue to evaluate code until the request returns.

          So when you put recno.focus() inside of doStuff:



          recno.focus() gets evaluated immediately after http.send gets executed, regardless of whether or (more likely) not the http request has returned.

          What you want to do instead is to include recno.focus() inside of handleHttpRespo nse, so that it doesn't get called until your AJAX call returns.

          [EDIT:
          E.g.:

          Code:
          function handleHttpResponse(response) {
          	.
          	.
          	.
          	//	recno is a global variable, right?
          	recno.focus();
          }
          ]
          Hi, thanks for your help. But the problem is I have the handleHttpRespo nse code being called by many functions and only want the focus executed in one of them.

          Recno is a field in a form, with different fuctions being called depending on which buttons the user clicks.

          Basically, I have an accounts receivable app that includes searches by various fields, and when a particular set of records is chosen I have controls for fwd/back and go directly to a record in the set. It's that "goto" box containing the current record number (recno) that I'd like to focus, but only if a search is being done. They can type directly into the box to choose a record. Sorry I can't put it online right now because I wouldn't want some of it available to the public. But there's an identical (exept for no AJAX) search at This Link .

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Originally posted by webhead
            But the problem is I have the handleHttpRespo nse code being called by many functions and only want the focus executed in one of them.
            You could use a different callback for each AJAX call.

            E.g.,

            Code:
            function initAjax(url, callback) {
            	http.open("GET", url, true);
            	http.onreadystatechange = callback;
            	http.send(null);
            }
            
            function doStuff() {
            	var recno = document.getElementById('recno');
            	
            	initAjax("moreStuff.php?recno=" + recno.value, function(response) {
            		.
            		.
            		.
            		recno.focus();
            	});
            }
            When initAjax gets called, you create an anonymous function that, (presumably) among other things, calls recno.focus(), and thanks to JavaScript's magical scope implementation, anything declared in doStuff is available to your callback function.

            If you wanted to make another AJAX call, but you wanted to do other stuff when the request returned, you'd do this:

            Code:
            function doOtherStuff() {
            	initAjax('yetMoreStuff.php?you=get&the=idea', function(response) {
            		.
            		.
            		.
            	});
            }
            [EDIT: Recno. What a great name. Come here, Recno! Here, boy! That's a GOOD boy, Recno! Who wants a treat? Does Recno want a treat? Awww what a cute widdle puppy!]

            Comment

            • webhead
              New Member
              • Mar 2006
              • 56

              #7
              Originally posted by pbmods
              You could use a different callback for each AJAX call.

              E.g.,

              Code:
              function initAjax(url, callback) {
              	http.open("GET", url, true);
              	http.onreadystatechange = callback;
              	http.send(null);
              }
              
              function doStuff() {
              	var recno = document.getElementById('recno');
              	
              	initAjax("moreStuff.php?recno=" + recno.value, function(response) {
              		.
              		.
              		.
              		recno.focus();
              	});
              }
              When initAjax gets called, you create an anonymous function that, (presumably) among other things, calls recno.focus(), and thanks to JavaScript's magical scope implementation, anything declared in doStuff is available to your callback function.

              If you wanted to make another AJAX call, but you wanted to do other stuff when the request returned, you'd do this:

              Code:
              function doOtherStuff() {
              	initAjax('yetMoreStuff.php?you=get&the=idea', function(response) {
              		.
              		.
              		.
              	});
              }
              [EDIT: Recno. What a great name. Come here, Recno! Here, boy! That's a GOOD boy, Recno! Who wants a treat? Does Recno want a treat? Awww what a cute widdle puppy!]
              Heck, I though it sounded more like an auto parts store ;-)

              Anyway, getting late and I'll see if I can try that tomorrow. Thanks!

              Comment

              • webhead
                New Member
                • Mar 2006
                • 56

                #8
                I must have something messed up, because now the page won't work at all (shows blank). Here's what I've got:
                Code:
                function initAjax(url, callback) {
                	http.open("GET", url, true);
                	http.onreadystatechange = callback;
                	http.send(null);
                	}
                
                function doStuff() {
                	recn=document.getElementById('recno').value;
                	initAjax("moreStuff.php?recn="+recn, function(response) {
                		http.open("GET", str, true);
                		http.onreadystatechange = handleHttpResponse;
                		http.send(null);
                		recno.focus();
                		});
                	}
                Was I supposed to put something else where you have "function(respo nse)"? Not sure what should go there.

                Comment

                • webhead
                  New Member
                  • Mar 2006
                  • 56

                  #9
                  Worked on it some and have this now:
                  Code:
                  function initAjax(url, callback) {
                  	http.open("GET", url, true);
                  	http.onreadystatechange = callback;
                  	http.send(null);
                  	}
                  function doStuff() {
                  	recn=document.getElementById('recno').value;
                  	initAjax("moreStuff.php?recn="+recn, function response(){
                  		http.open("GET", str, true);
                  		http.onreadystatechange = handleHttpResponse;
                  		http.send(null);
                  		recno.focus();
                  		});
                  	}
                  But now it won't let me change the record number in the box at all, and still doesn't focus.

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Code:
                    function initAjax(url, callback) {
                    	http.open("GET", url, true);
                    	http.onreadystatechange = callback;
                    	http.send(null);
                    	}
                    function doStuff() {
                    	recn=document.getElementById('recno').value;
                    	initAjax("moreStuff.php?recn="+recn, function response() {
                    		recno.focus();
                    		});
                    	}
                    Callback is what initAjax executes when http.onreadysta techange fires, so the anonymous function should only contain what you want to do when your Ajax call returns.

                    Looking at the code (I've got my AJAX calls so deeply encapsulated, I've forgotten what a basic xmlHttpRequest call looks like), I think the problem is that http is a global variable. You need to create a local variable inside initAjax and create a new xmlHttpRequest instance.

                    Code:
                    function initAjax(url, callback) {
                    	var http = new XMLHttpRequest();
                    	http.open("GET", url, true);
                    	http.onreadystatechange = callback;
                    	http.send(null);
                    }
                    Otherwise, every time you call initAjax, http gets destroyed and re-assigned a new request.

                    Comment

                    • webhead
                      New Member
                      • Mar 2006
                      • 56

                      #11
                      Houston, we have focus!

                      Thanks!

                      But...

                      I had it so clicking in the recno box would actually go and find the record. That's what the url pointed to, the Find code. But it doesn't execute the code, just does the focus.

                      Comment

                      • webhead
                        New Member
                        • Mar 2006
                        • 56

                        #12
                        It's looking like the old game of whack-a-mole: I can either get focus or find a record, but as soon as one thing works the other doesn't.

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          Originally posted by webhead
                          I had it so clicking in the recno box would actually go and find the record. That's what the url pointed to, the Find code. But it doesn't execute the code, just does the focus.
                          Let's have a look at the code.

                          Comment

                          • webhead
                            New Member
                            • Mar 2006
                            • 56

                            #14
                            Here it is:
                            Code:
                            /* moreStuff.php */
                            
                            <?php 
                            ...
                            $button = $_GET['button']; 
                            $recn = $_GET['recn'];
                            ...
                            // FIND SPECIFIC RECORD FROM GOTO BOX
                            if ($button=="goto") {
                            	...
                            	 // find record and track which page we're on
                            	}
                            ...
                            ?>
                            
                            <!--  DISPLAY FORM  -->
                            ...
                            	<img src='icons/goto.png' onClick='getGoto()'>
                            	<input name='recno' type='text' id='recno' value='<?php echo $page; ?>' size='4'> 
                            ...
                            
                            
                            /* moreStuff.js */
                            
                            function handleHttpResponse() {
                            	if (http.readyState == 4) {
                            		document.getElementById(theOutput).innerHTML = http.responseText;
                            		}
                            	}
                            
                            function getHTTPObject() {
                            	var xmlhttp;
                            	/*@cc_on
                            	@if (@_jscript_version >= 5)
                            		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
                            		catch (e) {
                            			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
                            	catch (E) { xmlhttp = false; }
                            			}
                            	@else
                            		xmlhttp = false;
                            		@end @*/
                            		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
                            			try { xmlhttp = new XMLHttpRequest(); } 
                            			catch (e) { xmlhttp = false; }
                            			}
                            		return xmlhttp;
                            	}
                            
                            var http = getHTTPObject(); 
                            
                            theOutput = 'formbox';
                            ...
                            
                            function initAjax(url, callback) {
                            	var http = new XMLHttpRequest();
                            	http.open("GET", url, true);
                            	http.onreadystatechange = callback;
                            	http.send(null);
                            	}
                            function getGoto() {
                            	recn=document.getElementById('recno').value;
                            	initAjax("moreStuff.php?button=goto&recn="+recn, function response(){
                            		recno.focus(); 
                            		});
                            	}

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Couple of things to change:

                              Code:
                              //var http = getHTTPObject(); 
                              .
                              .
                              .
                              function initAjax(url, callback) {
                              	var http = getHTTPObject();
                              .
                              .
                              .
                              	}
                              
                              function getGoto() {
                              .
                              .
                              .
                              	initAjax("moreStuff.php?button=goto&recn="+recn, function (response) {
                              .
                              .
                              .
                              	}
                              Since you might be making multiple AJAX calls concurrently, you don't want http to be a global. Instead, let initAjax create a new (local) http variable and send the request; that way, even if the first http hasn't returned yet, you can still call initAjax and init another request.

                              As far as the callback syntax goes, you're creating what's called an anonymous function, so therefore it doesn't have a name.

                              Code:
                              function name(args) {implementation}
                              is actually a synonym for

                              Code:
                              name = new Function(args, implementation);
                              The 'response' in the callback function is an argument that stores the returned object from your Ajax call. XMLHttpRequest does this by default. You could remove 'response' entirely from your anonymous function, if you wanted, though I like to keep it just so that there's that much less work to do if I decide I need to process the response from the server.

                              This would work equally as well, though:

                              Code:
                              initAjax("...", function () {
                              });

                              Comment

                              Working...