473,804 Members | 3,424 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
33 3987
webhead
56 New Member
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.
May 12 '07 #11
webhead
56 New Member
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.
May 12 '07 #12
pbmods
5,821 Recognized Expert Expert
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.
May 12 '07 #13
webhead
56 New Member
Here it is:
Expand|Select|Wrap|Line Numbers
  1. /* moreStuff.php */
  2.  
  3. <?php 
  4. ...
  5. $button = $_GET['button']; 
  6. $recn = $_GET['recn'];
  7. ...
  8. // FIND SPECIFIC RECORD FROM GOTO BOX
  9. if ($button=="goto") {
  10.     ...
  11.      // find record and track which page we're on
  12.     }
  13. ...
  14. ?>
  15.  
  16. <!--  DISPLAY FORM  -->
  17. ...
  18.     <img src='icons/goto.png' onClick='getGoto()'>
  19.     <input name='recno' type='text' id='recno' value='<?php echo $page; ?>' size='4'> 
  20. ...
  21.  
  22.  
  23. /* moreStuff.js */
  24.  
  25. function handleHttpResponse() {
  26.     if (http.readyState == 4) {
  27.         document.getElementById(theOutput).innerHTML = http.responseText;
  28.         }
  29.     }
  30.  
  31. function getHTTPObject() {
  32.     var xmlhttp;
  33.     /*@cc_on
  34.     @if (@_jscript_version >= 5)
  35.         try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
  36.         catch (e) {
  37.             try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
  38.     catch (E) { xmlhttp = false; }
  39.             }
  40.     @else
  41.         xmlhttp = false;
  42.         @end @*/
  43.         if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  44.             try { xmlhttp = new XMLHttpRequest(); } 
  45.             catch (e) { xmlhttp = false; }
  46.             }
  47.         return xmlhttp;
  48.     }
  49.  
  50. var http = getHTTPObject(); 
  51.  
  52. theOutput = 'formbox';
  53. ...
  54.  
  55. function initAjax(url, callback) {
  56.     var http = new XMLHttpRequest();
  57.     http.open("GET", url, true);
  58.     http.onreadystatechange = callback;
  59.     http.send(null);
  60.     }
  61. function getGoto() {
  62.     recn=document.getElementById('recno').value;
  63.     initAjax("moreStuff.php?button=goto&recn="+recn, function response(){
  64.         recno.focus(); 
  65.         });
  66.     }
May 12 '07 #14
pbmods
5,821 Recognized Expert Expert
Couple of things to change:

Expand|Select|Wrap|Line Numbers
  1. //var http = getHTTPObject(); 
  2. .
  3. .
  4. .
  5. function initAjax(url, callback) {
  6.     var http = getHTTPObject();
  7. .
  8. .
  9. .
  10.     }
  11.  
  12. function getGoto() {
  13. .
  14. .
  15. .
  16.     initAjax("moreStuff.php?button=goto&recn="+recn, function (response) {
  17. .
  18. .
  19. .
  20.     }
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.

Expand|Select|Wrap|Line Numbers
  1. function name(args) {implementation}
is actually a synonym for

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. initAjax("...", function () {
  2. });
  3.  
May 12 '07 #15
webhead
56 New Member
Couple of things to change:

Expand|Select|Wrap|Line Numbers
  1. //var http = getHTTPObject(); 
  2. .
  3. .
  4. .
  5. function initAjax(url, callback) {
  6.     var http = getHTTPObject();
  7. .
  8. .
  9. .
  10.     }
Moved the "var http = getHTTPObject() ;" to initAjax, but since it's no longer global I assume I must add that line to any other functions, right? My page won't load at all so I went ahead and added it to other functions, but I must have it in the wrong place since it still won't load. Example:
Expand|Select|Wrap|Line Numbers
  1. function nutherFunction(theButton) {
  2.     str = "moreStuff.php?button="+theButton;
  3.         // add line here
  4.         var http = getHTTPObject();
  5.     //
  6.         http.open("GET", str, true);
  7.     http.onreadystatechange = handleHttpResponse;
  8.     http.send(null);
  9.     }
May 12 '07 #16
pbmods
5,821 Recognized Expert Expert
Moved the "var http = getHTTPObject() ;" to initAjax, but since it's no longer global I assume I must add that line to any other functions, right?
The idea is to use initAjax to handle all of your AJAX calls. In other words:

Expand|Select|Wrap|Line Numbers
  1. function doSomething() {
  2.     initAjax('thepage.php?arg=val', function(ajax_response) {
  3.         alert(ajax_response.responseText);
  4.         recno.focus();
  5.     });
  6. }
  7.  
  8. function doSomethingElse() {
  9.     initAjax('anotherpage.php?and=so&on=', function(ajax_response) {
  10.         recno.fetch();
  11.         recno.rollOver();
  12.         recno.playDead();
  13.  
  14.         //    Or if you prefer...
  15.         recno.whaddyaMeanMyTransmissionIsShotAllIWantedWasAnOilChange();
  16.     });
  17. }
  18.  
In the examples above, doSomething and doSomethingElse call initAjax, which creates the XMLHttpRequest object and then executes the callback function. So neither doSomething nor doSomethingElse has to deal with any of that.
May 13 '07 #17
webhead
56 New Member
Aaaahhhhh, I see. (Can't tell I'm a n00b at AJAX can ya? Or that I even had anything working at all just by copy/paste from sketchy tutorials?)

So now it goes a little something like this:

Expand|Select|Wrap|Line Numbers
  1. function getFunky() {
  2.     url = "doStuff.php?button=add";
  3.     initAjax(url, function response(){ --- stuff here if needed --- });
  4.     }
And "function response" can be empty between the {} if I don't have other things to do after that, right?

But... still no page loading. Checking code for typos...
May 13 '07 #18
webhead
56 New Member
This can't be right:
Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     var http = getHTTPObject(); 
  3.     var http = new XMLHttpRequest();
  4.     http.open("GET", url, true);
  5.     http.onreadystatechange = callback;
  6.     http.send(null);
  7.     }
May 13 '07 #19
pbmods
5,821 Recognized Expert Expert
Aaaahhhhh, I see. (Can't tell I'm a n00b at AJAX can ya? Or that I even had anything working at all just by copy/paste from sketchy tutorials?)
We've all been there. With time, all things pass :)

And "function response" can be empty between the {} if I don't have other things to do after that, right?
Alrightey. Buckle down and forget everything you learned about JavaScript. Here we go.

JavaScript is fiercely object-oriented. Just about everything is an object. 2 is an instance of the Number class. 'Hello, World!' is an instance of the String class. And getFunky (in the example above) is an instance of the Function class.

As I noted above, when you type this:

Expand|Select|Wrap|Line Numbers
  1. function getFunky(mark) { return mark * 5; }
  2.  
JavaScript treats it like this:
Expand|Select|Wrap|Line Numbers
  1. getFunky = new Function('mark', 'return mark * 5;');
  2.  
The browser creates a new global variable called 'getFunky', and assigns it an instance of the Function class with one argument ('mark'), and a function body ('return mark * 5;');

The two statements are equivalent, but the browser will automatically use the latter form at runtime.

Remember that. All functions in JavaScript are actually Function objects.

Ok, so with that in mind, consider this: When you invoke a function, and you pass arguments to that function, sometimes you use 'literals' instead of variables. Here's an example:

Expand|Select|Wrap|Line Numbers
  1. doSomething(5);
  2.  
  3. var mark = 5;
  4. doSomething(mark);
  5.  
In both cases, you're passing the number 5 to doSomething. But in the first statement, you use the 'literal' 5, whereas in the second set of instructions, you're passing the variable 'mark', which has a value of 5.

So far so good?

Now, since in JavaScript, the number 5 is actually an instance of the Number class, you could also do this:

Expand|Select|Wrap|Line Numbers
  1. doSomething(new Number(5));
  2.  
This statement creates a new instance of the Number class, assigns it the value of 5 and passes it to doSomething.

Ok. Now, instead of a Number object, let's use a Function object. We'll start with an easy one:

Expand|Select|Wrap|Line Numbers
  1. function myFunc(x) { return x * 5; }
  2. //  Which is the same as:
  3. //  myFunc = new Function('x', 'return x * 5;');
  4.  
  5. alert(myFunc);
  6.  
What happens here, keeping in mind that myFunc is a variable that stores a Function object?

The output of the alert statement, btw, is outside the scope of this post, so we'll just move on to the next part:

Expand|Select|Wrap|Line Numbers
  1. //  Remember from before where we passed a literal to a function:
  2. alert(10);
  3. alert(new Number(10));
  4. //  Or...
  5. alert('Hello, World!');
  6. alert(new String('Hello, World!'));
  7.  
  8. //  We can also pass a Function 'literal':
  9. alert(function(x) { return x * 5; });
  10. alert(new Function('x', 'return x * 5;'));
  11.  
Note that, just like 10 or 'Hello, World!', there is no variable that stores the function you created in those last two statements; they are called "anonymous functions".

Still with me?

So going back to your code, when you type:

Expand|Select|Wrap|Line Numbers
  1. initAjax(url, function(response) {
  2. });
  3.  
which is identical to this:

Expand|Select|Wrap|Line Numbers
  1. initAjax(url, new Function('response', ''));
  2.  
You're actually creating a new anonymous function which takes one argument (response, which will store the returned object from your XMLHttpRequest call automatically [and you don't have to worry about it; just assume that it will]).

Now, let's take a look at initAjax:

Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback)
  2.  
Note that url is a String object, and callback is a Function object. callback stores the anonymous function that you created when you invoked initAjax in the last code block.

Likewise, when you do this:

Expand|Select|Wrap|Line Numbers
  1. http.onreadystatechange = callback;
  2.  
You are copying the function stored in callback to http.onreadysta techange. When http's readystate changes, it will check to see if there is a function stored in its onreadystatecha nge property, which in this case is (technically a copy of) the anonymous function you created when you invoked initAjax.

Hope that answered a few more questions than it generated.

And also, you are correct:

This can't be right:
Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     var http = getHTTPObject(); 
  3.     var http = new XMLHttpRequest();
  4.     http.open("GET", url, true);
  5.     http.onreadystatechange = callback;
  6.     http.send(null);
  7.     }
Get rid of this line:
Expand|Select|Wrap|Line Numbers
  1. var http = new XMLHttpRequest();
Since you brought in the getHTTPObject function, you no longer need that line. getHTTPObject will create a new XMLHttpRequest object (or some Microsoft thing if you're using IE) and return it.

In this case, initAjax stores the returned object to the variable http.

Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     var http = getHTTPObject(); 
  3.     http.open("GET", url, true);
  4.     http.onreadystatechange = callback;
  5.     http.send(null);
  6.     }
May 13 '07 #20

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

Similar topics

1
1767
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
1193
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
3792
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
4910
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
7359
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
2150
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
2264
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
6056
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
6905
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
9710
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
10593
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
10340
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
10329
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
9163
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
7626
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
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4304
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
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.