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
We've all been there. With time, all things pass :)
Yes, but now and then you need a little Ex-Lax to get things moving ;-)

Alrightey. Buckle down and forget everything you learned about JavaScript. Here we go....

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?
So anything "new" creates a variable that holds a string, number, or function, right? So if I pass an empty function { } it's just like passing an empty string.

...

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]).
A little fuzzy on how that works but right now I'm happy to take your word for it.

Question: We used to do
Expand|Select|Wrap|Line Numbers
  1. http.onreadystatechange = http.responseText;;
  2.  
instead of
Expand|Select|Wrap|Line Numbers
  1. http.onreadystatechange = callback;
  2.  
. When do we do http.responseTe xt now?

Still no page load, so I started the old put alerts everywhere thing, and it seems to go through all the right motions, yet nothing is put out. I tried it also on a much simpler part of my app, a section in another frame that just puts out a "rate card" of values used in calculations for an invoice. It displays the numbers and takes any changes, only this and nothing more.

Yet no output. But it's small so I'll try and post the whole thing here:
Expand|Select|Wrap|Line Numbers
  1. theOutput = 'output'; 
  2. function handleHttpResponse() {
  3.     if (http.readyState == 4) {
  4.         document.getElementById(theOutput).innerHTML = http.responseText;
  5.         }
  6.     }
  7.  
  8. function getHTTPObject() {
  9.     var xmlhttp;
  10.     /*@cc_on
  11.     @if (@_jscript_version >= 5)
  12.         try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
  13.         catch (e) {
  14.             try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
  15.             catch (E) { xmlhttp = false; }
  16.             }
  17.     @else
  18.         xmlhttp = false;
  19.         @end @*/
  20.         if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  21.             try { xmlhttp = new XMLHttpRequest(); } 
  22.             catch (e) { xmlhttp = false; }
  23.             }
  24.         return xmlhttp;
  25.     }
  26.  
  27. function initAjax(url, callback) {
  28.     var http = getHTTPObject(); 
  29.     http.open("GET", url, true);
  30.     http.onreadystatechange = callback;
  31.     http.send(null);
  32.     }
  33. /////////////////////////////////////////////////////////////////////////////////
  34. function getRatecard() {
  35.     url = "Rfunc.php";
  36.     initAjax(url, function response(){ });
  37.     }    
  38.  
  39. function rChange(theField) {
  40.     fldnam=document.getElementById(theField).name;
  41.     fldval=document.getElementById(theField).value;
  42.     url = 'Rfunc.php?fldn='+fldnam+'&fldv='+fldval; 
  43.     initAjax(url, function response(){ });
  44.     } 
  45.  
  46. function rReport() {
  47.     url = 'Rfunc.php?report=recvbls'; 
  48.     initAjax(url, function response(){ });
  49.     } 
May 13 '07 #21
webhead
56 New Member
Interesting:
Expand|Select|Wrap|Line Numbers
  1. function getRatecard() {
  2.     url = "Rfunc.php";
  3.     initAjax(url, function response(){ document.getElementById(theOutput).innerHTML = http.responseText; }); 
  4.     }    
puts out nothing but:
Expand|Select|Wrap|Line Numbers
  1. function getRatecard() {
  2.     url = "Rfunc.php";
  3.     initAjax(url, function response(){ document.getElementById(theOutput).innerHTML = 'blah'; }); 
  4.     }    
puts out "blah". Looks like we just aren't putting anything into ...innerHTML, because by the time we get to that statement the responseText is gone.
May 13 '07 #22
pbmods
5,821 Recognized Expert Expert
Go back and reread my last post.
May 13 '07 #23
pbmods
5,821 Recognized Expert Expert
So anything "new" creates a variable that holds a string, number, or function, right? So if I pass an empty function { } it's just like passing an empty string.
Basically, considering that when you pass an empty string, you're actually passing an instance of the String class without a value:

Expand|Select|Wrap|Line Numbers
  1. doSomething('') === doSomething(new String(''));
  2. doSomething(function() {}) === doSomething(new Function('', ''));
  3.  
Question: We used to do
Expand|Select|Wrap|Line Numbers
  1. http.onreadystatechange = http.responseText;;
  2.  
instead of
Expand|Select|Wrap|Line Numbers
  1. http.onreadystatechange = callback;
  2.  
. When do we do http.responseTe xt now?
This is where that 'response' that you keep confusing for the function's name comes into play.

NOTE WHERE I PUT 'RESPONSE'. IT GOES INSIDE THE PARENTHESIS.

Expand|Select|Wrap|Line Numbers
  1. initAjax(url, function (response) { eval(response.responseText); });
  2.  
  3. initAjax(url, new Function('response', 'eval(response.responseText);'));
  4.  
The two statements do exactly the same thing. Remember that when http.onreadysta techange fires, it will automatically pass the result as the first parameter.

Fix your callbacks, and things should start working.
May 13 '07 #24
webhead
56 New Member
Oops, how did I do that with the ()? Fixed, added the eval, but still nothing. Tried this too and still nothing:
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.     }
  7.  
  8. function getRatecard() {
  9.     url = "Rfunc.php";
  10.     initAjax(url, function (response) { 
  11.         eval(response.responseText); 
  12.         document.getElementById(theOutput).innerHTML = response.responseText;
  13.         }); 
  14.     }    
Sorry I'm not catching on too well, maybe I need to go back and find some more beginner tutorials. But you've been a most patient teacher and I am very grateful for your help.
May 13 '07 #25
pbmods
5,821 Recognized Expert Expert
Oops, how did I do that with the ()? Fixed, added the eval, but still nothing. Tried this too and still nothing:
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.     }
  7.  
  8. function getRatecard() {
  9.     url = "Rfunc.php";
  10.     initAjax(url, function (response) { 
  11.         eval(response.responseText); 
  12.         document.getElementById(theOutput).innerHTML = response.responseText;
  13.         }); 
  14.     }    
Ok, this is looking pretty good. But now we have a conflict:

Expand|Select|Wrap|Line Numbers
  1.         eval(response.responseText); 
  2.         document.getElementById(theOutput).innerHTML = response.responseText;
  3.  
I used the 'eval' line just as an example, because I wasn't sure what you were getting back from your server-side script. But it looks like the script is sending HTML, not JavaScript. So you can go ahead and remove the 'eval' line (since eval(html code) will produce an error):

Expand|Select|Wrap|Line Numbers
  1. function getRatecard() {
  2.     url = "Rfunc.php";
  3.     initAjax(url, function (response) { 
  4.         document.getElementById(theOutput).innerHTML = response.responseText;
  5.         }); 
  6.     }
Sorry I'm not catching on too well, maybe I need to go back and find some more beginner tutorials. But you've been a most patient teacher and I am very grateful for your help.
I'm having fun, I promise. I know we all make typos occasionally (I happen to hold the world record for typos per hour, and I broke it three times last weeek^K^Kk). I apologize for my outburst.
May 13 '07 #26
webhead
56 New Member
Expand|Select|Wrap|Line Numbers
  1. function getRatecard() {
  2.     url = "Rfunc.php";
  3.     initAjax(url, function (response) { 
  4.         document.getElementById(theOutput).innerHTML = response.responseText;
  5.         }); 
  6.     }
Done, but still nothing from "response.respo nseText;". Is it possible we need to wait for "http.readyStat e == 4"? I tried putting that around the "...innerHTML.. ." line but still nothing.

I'm having fun, I promise. I know we all make typos occasionally (I happen to hold the world record for typos per hour, and I broke it three times last weeek^K^Kk). I apologize for my outburst.
No prob. I know I can get on people's nerves with endless questions, especially when it's free edjamakashun. Thanks again.
May 13 '07 #27
pbmods
5,821 Recognized Expert Expert
Done, but still nothing from "response.respo nseText;". Is it possible we need to wait for "http.readyStat e == 4"?
Ooh. Good point. Alrightey. Let's add another layer of complexity to initAjax:

Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     var http = getHTTPObject(); 
  3.     http.open("GET", url, true); 
  4.     http.onreadystatechange = function(response) {
  5.         if(response.readyState == 4)
  6.             callback(response);
  7.     };
  8.     http.send(null);
  9.     }
  10.  
We create a new anonymous function that checks for response's readyState (remember that response is set to the result from the AJAX call automatically when you assign it to http.onreadysta techange; http is undefined outside of initAjax, so we pass the response through callbacks). If it is 4, then we call callback and pass the response to it. Otherwise, we do nothing.

I tried putting that around the "...innerHTML.. ." line but still nothing.
Hm.

Try alerting your responseText to see if your script is sending anything back to you.

Expand|Select|Wrap|Line Numbers
  1. initAjax(url, function (response) { 
  2.         alert(response.responseText);
  3.         }); 
  4.  
May 14 '07 #28
webhead
56 New Member
Try alerting your responseText to see if your script is sending anything back to you.
Expand|Select|Wrap|Line Numbers
  1. initAjax(url, function (response) { 
  2.         alert(response.responseText);
  3.         }); 
  4.  
Tried this:
Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     var http = getHTTPObject(); 
  3.     http.open("GET", url, true);
  4.     http.onreadystatechange = function(response) {
  5.         if (response.readyState == 4) { callback(response); } 
  6.         };
  7.     alert('ack');
  8.     http.send(null);
  9.     }
  10.  
  11. function getTestpage() { 
  12.     url = "afunc.php";
  13.     initAjax(url, function (response) { 
  14.             document.getElementById(theOutput).innerHTML = response.responseText;
  15.             alert(response.responseText);
  16.         });
  17.     alert('done');
  18.     }    
I get alerts for "ack" and "done" but not "response.respo nseText". Not even a blank alert, no alert at all.
May 14 '07 #29
pbmods
5,821 Recognized Expert Expert
Tried this:
Expand|Select|Wrap|Line Numbers
  1. function initAjax(url, callback) {
  2.     var http = getHTTPObject(); 
  3.     http.open("GET", url, true);
  4.     http.onreadystatechange = function(response) {
  5.         if (response.readyState == 4) { callback(response); } 
  6.         };
  7.     alert('ack');
  8.     http.send(null);
  9.     }
  10.  
  11. function getTestpage() { 
  12.     url = "afunc.php";
  13.     initAjax(url, function (response) { 
  14.             document.getElementById(theOutput).innerHTML = response.responseText;
  15.             alert(response.responseText);
  16.         });
  17.     alert('done');
  18.     }    
I get alerts for "ack" and "done" but not "response.respo nseText". Not even a blank alert, no alert at all.
Here's why you're not seeing anything:

heh

Because http actually *isn't* automatically assigning a value to response. I went back and looked at my AJAX frameworks, and it turns out that you have to manually do this.

Yeah. *sheepish grin*

So anyway, here's what initAjax *really* should look like (are we having fun yet?):

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

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
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
10085
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
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...
0
5527
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...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.