473,795 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Focus in Textbox when the page is Loaded through Ajax

14 New Member
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: ****
Feb 25 '07 #1
33 3983
acoder
16,027 Recognized Expert Moderator MVP
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.
Feb 26 '07 #2
webhead
56 New Member
I'm having the same problem but tried doing the focus in the function and it still doesn't work. Here's my javascript:
Expand|Select|Wrap|Line Numbers
  1. function doStuff() {
  2.     recn=document.getElementById('recno').value;
  3.     str = "moreStuff.php?recn="+recn;
  4.     http.open("GET", str, true);
  5.     http.onreadystatechange = handleHttpResponse;
  6.     http.send(null);
  7.     recno.focus();
  8.     }
Is this the wrong place to call for focus, or do I have the call syntax wrong?
May 11 '07 #3
pbmods
5,821 Recognized Expert Expert
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:

Expand|Select|Wrap|Line Numbers
  1.     http.send(null);
  2.     recno.focus();
  3.     }
  4.  
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.:

Expand|Select|Wrap|Line Numbers
  1. function handleHttpResponse(response) {
  2.     .
  3.     .
  4.     .
  5.     //    recno is a global variable, right?
  6.     recno.focus();
  7. }
  8.  
]
May 11 '07 #4
webhead
56 New Member
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.:

Expand|Select|Wrap|Line Numbers
  1. function handleHttpResponse(response) {
  2.     .
  3.     .
  4.     .
  5.     //    recno is a global variable, right?
  6.     recno.focus();
  7. }
  8.  
]
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 .
May 12 '07 #5
pbmods
5,821 Recognized Expert Expert
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.,

Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     http.open("GET", url, true);
  3.     http.onreadystatechange = callback;
  4.     http.send(null);
  5. }
  6.  
  7. function doStuff() {
  8.     var recno = document.getElementById('recno');
  9.  
  10.     initAjax("moreStuff.php?recno=" + recno.value, function(response) {
  11.         .
  12.         .
  13.         .
  14.         recno.focus();
  15.     });
  16. }
  17.  
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:

Expand|Select|Wrap|Line Numbers
  1. function doOtherStuff() {
  2.     initAjax('yetMoreStuff.php?you=get&the=idea', function(response) {
  3.         .
  4.         .
  5.         .
  6.     });
  7. }
  8.  
[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!]
May 12 '07 #6
webhead
56 New Member
You could use a different callback for each AJAX call.

E.g.,

Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     http.open("GET", url, true);
  3.     http.onreadystatechange = callback;
  4.     http.send(null);
  5. }
  6.  
  7. function doStuff() {
  8.     var recno = document.getElementById('recno');
  9.  
  10.     initAjax("moreStuff.php?recno=" + recno.value, function(response) {
  11.         .
  12.         .
  13.         .
  14.         recno.focus();
  15.     });
  16. }
  17.  
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:

Expand|Select|Wrap|Line Numbers
  1. function doOtherStuff() {
  2.     initAjax('yetMoreStuff.php?you=get&the=idea', function(response) {
  3.         .
  4.         .
  5.         .
  6.     });
  7. }
  8.  
[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!
May 12 '07 #7
webhead
56 New Member
I must have something messed up, because now the page won't work at all (shows blank). Here's what I've got:
Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     http.open("GET", url, true);
  3.     http.onreadystatechange = callback;
  4.     http.send(null);
  5.     }
  6.  
  7. function doStuff() {
  8.     recn=document.getElementById('recno').value;
  9.     initAjax("moreStuff.php?recn="+recn, function(response) {
  10.         http.open("GET", str, true);
  11.         http.onreadystatechange = handleHttpResponse;
  12.         http.send(null);
  13.         recno.focus();
  14.         });
  15.     }
Was I supposed to put something else where you have "function(respo nse)"? Not sure what should go there.
May 12 '07 #8
webhead
56 New Member
Worked on it some and have this now:
Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     http.open("GET", url, true);
  3.     http.onreadystatechange = callback;
  4.     http.send(null);
  5.     }
  6. function doStuff() {
  7.     recn=document.getElementById('recno').value;
  8.     initAjax("moreStuff.php?recn="+recn, function response(){
  9.         http.open("GET", str, true);
  10.         http.onreadystatechange = handleHttpResponse;
  11.         http.send(null);
  12.         recno.focus();
  13.         });
  14.     }
  15.  
But now it won't let me change the record number in the box at all, and still doesn't focus.
May 12 '07 #9
pbmods
5,821 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     http.open("GET", url, true);
  3.     http.onreadystatechange = callback;
  4.     http.send(null);
  5.     }
  6. function doStuff() {
  7.     recn=document.getElementById('recno').value;
  8.     initAjax("moreStuff.php?recn="+recn, function response() {
  9.         recno.focus();
  10.         });
  11.     }
  12.  
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.

Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     var http = new XMLHttpRequest();
  3.     http.open("GET", url, true);
  4.     http.onreadystatechange = callback;
  5.     http.send(null);
  6. }
Otherwise, every time you call initAjax, http gets destroyed and re-assigned a new request.
May 12 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1765
by: Gill Smith | last post by:
when the page is loaded I am using onload event at the client side and setting the focus on the control(TextBox). However when the user clicks on Clear ImageButton, I am clearing all the text boxes and the grid(previous search result display) in the clicked event of the ImageButton at the server side. How to set the focus on the TextBox again when the user clicks on the Clear button. -Gill
3
1190
by: A.M | last post by:
Hi, Can I configure a TextBox to have focus and cursor on it when page gets loaded? Thanks, Alan
4
3791
by: Christian Ista | last post by:
Hello, I have 2 questions : 1. On an ASP.NET page I have several controls (5 TextBox, 1 Dropdown and 1 button) Only the dropdown is AutoPostBack = true, the TextBox are SingleLine When I execute the page, I fill in the textbox, I change the dropdown selection, the page is reloaded no problem I see the textbox still fill in.
12
4909
by: CLEAR-RCIC | last post by:
Hi, I'm having problems setting focus to a textbox on a web user contol on an asp.net web page. The following script works on normal asp.net pages: <script language="javascript"> function cmdButton1_Clicked() { document.all('txtInput1').focus(); return false; }
11
7347
by: Alex.Svetos | last post by:
Hello, I'm trying to get a popup to keep focus when it is re-clicked. The script below is supposed to produce this exact behaviour, however it doesn't work, at least on firefox 1.0.7 and moz 1.7.12 (linux kubuntu). It does work with konqueror. It seems to work with firefox on windows but not with IE (not completly sure though).
3
2149
by: DJTN | last post by:
I have an IE web control on a form that rotates the stats of the call center in it. When a new page is loaded it takes the focus away from the other textboxes on the form, even if a user is typing in them. Is there anyway to keep the control from getting focus?
8
2263
by: zacware | last post by:
I have an AJAX enabled page which contains a list of records out of a database which are loaded into a div via an AJAX call. If the user switches to another window, and comes back to this open window later on, I want the list of records to be udpated when the window is back in front a simple onFocus doesn't work, as what happens is that if a button is clicked on the window it causes the onfocus to fire as well
4
6055
by: pablorp80 | last post by:
Hello, Here is what I need: I need the focus and the cursor set to a textbox named txtGT, every time no matter if it is the first page load or whether it is a postback. Here is the problem: I am using AJAX and MasterPages as well as an update panel, the textbox is in a panel. I have tried to do it using different java scripts but I can't get it to work because I am not using asp forms, instead I am using Containers. Here is my code:...
8
6904
by: Mel | last post by:
I have several text boxes and drop-down lists in an AJAX Update Panel. All user inputs have the Postback property set to True. After I type something in the first input entry and press the "Tab" key how can I set the focus to the next box after the postback? Please help! Using Visual Studio 2005 Pro, Asp.net 2.0, vb.net, WinXP
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.