Focus in Textbox when the page is Loaded through Ajax | Newbie | | Join Date: Feb 2007
Posts: 14
| | |
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="document.formname.textboxname.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: ****
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Focus in Textbox when the page is Loaded through Ajax
If you are using Ajax, use the onreadystatechange 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.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
I'm having the same problem but tried doing the focus in the function and it still doesn't work. Here's my javascript : - 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?
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
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, handleHttpResponse 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: Quote:
Originally Posted by webhead -
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 handleHttpResponse, so that it doesn't get called until your AJAX call returns.
[EDIT:
E.g.: -
function handleHttpResponse(response) {
-
.
-
.
-
.
-
// recno is a global variable, right?
-
recno.focus();
-
}
-
]
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by pbmods The first 'A' in AJAX stands for 'Asynchronous'. In other words, handleHttpResponse 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 handleHttpResponse, so that it doesn't get called until your AJAX call returns.
[EDIT:
E.g.: -
function handleHttpResponse(response) {
-
.
-
.
-
.
-
// recno is a global variable, right?
-
recno.focus();
-
}
-
] Hi, thanks for your help. But the problem is I have the handleHttpResponse 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 .
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead But the problem is I have the handleHttpResponse 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., -
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: -
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!]
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by pbmods You could use a different callback for each AJAX call.
E.g., -
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: -
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!
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
I must have something messed up, because now the page won't work at all (shows blank). Here's what I've got: - 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(response)"? Not sure what should go there.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
Worked on it some and have this now: - 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.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax - 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.onreadystatechange 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. - 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.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
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.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
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.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
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.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
Here it is: - /* 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();
-
});
-
}
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax
Couple of things to change: -
//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. - function name(args) {implementation}
is actually a synonym for - 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: -
initAjax("...", function () {
-
});
-
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by pbmods Couple of things to change: -
//var http = getHTTPObject();
-
.
-
.
-
.
-
function initAjax(url, callback) {
-
var http = getHTTPObject();
-
.
-
.
-
.
-
}
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: - function nutherFunction(theButton) {
-
str = "moreStuff.php?button="+theButton;
-
// add line here
-
var http = getHTTPObject();
-
//
-
http.open("GET", str, true);
-
http.onreadystatechange = handleHttpResponse;
-
http.send(null);
-
}
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead 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: -
function doSomething() {
-
initAjax('thepage.php?arg=val', function(ajax_response) {
-
alert(ajax_response.responseText);
-
recno.focus();
-
});
-
}
-
-
function doSomethingElse() {
-
initAjax('anotherpage.php?and=so&on=', function(ajax_response) {
-
recno.fetch();
-
recno.rollOver();
-
recno.playDead();
-
-
// Or if you prefer...
-
recno.whaddyaMeanMyTransmissionIsShotAllIWantedWasAnOilChange();
-
});
-
}
-
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.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
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: - function getFunky() {
-
url = "doStuff.php?button=add";
-
initAjax(url, function response(){ --- stuff here if needed --- });
-
}
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...
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
This can't be right: - function initAjax(url, callback) {
-
var http = getHTTPObject();
-
var http = new XMLHttpRequest();
-
http.open("GET", url, true);
-
http.onreadystatechange = callback;
-
http.send(null);
-
}
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead 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 :) Quote:
Originally Posted by webhead 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: -
function getFunky(mark) { return mark * 5; }
-
JavaScript treats it like this: -
getFunky = new Function('mark', 'return mark * 5;');
-
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: -
doSomething(5);
-
-
var mark = 5;
-
doSomething(mark);
-
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: -
doSomething(new Number(5));
-
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: -
function myFunc(x) { return x * 5; }
-
// Which is the same as:
-
// myFunc = new Function('x', 'return x * 5;');
-
-
alert(myFunc);
-
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: -
// Remember from before where we passed a literal to a function:
-
alert(10);
-
alert(new Number(10));
-
// Or...
-
alert('Hello, World!');
-
alert(new String('Hello, World!'));
-
-
// We can also pass a Function 'literal':
-
alert(function(x) { return x * 5; });
-
alert(new Function('x', 'return x * 5;'));
-
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: -
initAjax(url, function(response) {
-
});
-
which is identical to this: -
initAjax(url, new Function('response', ''));
-
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: -
function initAjax(url, callback)
-
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: -
http.onreadystatechange = callback;
-
You are copying the function stored in callback to http.onreadystatechange. When http's readystate changes, it will check to see if there is a function stored in its onreadystatechange 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: Quote:
Originally Posted by webhead This can't be right: -
function initAjax(url, callback) {
-
var http = getHTTPObject();
-
var http = new XMLHttpRequest();
-
http.open("GET", url, true);
-
http.onreadystatechange = callback;
-
http.send(null);
-
}
Get rid of this line: - 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. -
function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = callback;
-
http.send(null);
-
}
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by pbmods 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 ;-) Quote:
Alrightey. Buckle down and forget everything you learned about JavaScript. Here we go.... -
function myFunc(x) { return x * 5; }
-
// Which is the same as:
-
// myFunc = new Function('x', 'return x * 5;');
-
-
alert(myFunc);
-
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.
... Quote: -
initAjax(url, new Function('response', ''));
-
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 - http.onreadystatechange = http.responseText;;
-
instead of - http.onreadystatechange = callback;
-
. When do we do http.responseText 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: - theOutput = 'output';
-
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;
-
}
-
-
function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = callback;
-
http.send(null);
-
}
-
/////////////////////////////////////////////////////////////////////////////////
-
function getRatecard() {
-
url = "Rfunc.php";
-
initAjax(url, function response(){ });
-
}
-
-
function rChange(theField) {
-
fldnam=document.getElementById(theField).name;
-
fldval=document.getElementById(theField).value;
-
url = 'Rfunc.php?fldn='+fldnam+'&fldv='+fldval;
-
initAjax(url, function response(){ });
-
}
-
-
function rReport() {
-
url = 'Rfunc.php?report=recvbls';
-
initAjax(url, function response(){ });
-
}
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
Interesting: - function getRatecard() {
-
url = "Rfunc.php";
-
initAjax(url, function response(){ document.getElementById(theOutput).innerHTML = http.responseText; });
-
}
puts out nothing but: - function getRatecard() {
-
url = "Rfunc.php";
-
initAjax(url, function response(){ document.getElementById(theOutput).innerHTML = 'blah'; });
-
}
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.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax
Go back and reread my last post.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead 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: -
doSomething('') === doSomething(new String(''));
-
doSomething(function() {}) === doSomething(new Function('', ''));
-
Quote:
Question: We used to do - http.onreadystatechange = http.responseText;;
-
instead of - http.onreadystatechange = callback;
-
. When do we do http.responseText 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. -
initAjax(url, function (response) { eval(response.responseText); });
-
-
initAjax(url, new Function('response', 'eval(response.responseText);'));
-
The two statements do exactly the same thing. Remember that when http.onreadystatechange fires, it will automatically pass the result as the first parameter.
Fix your callbacks, and things should start working.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax
Oops, how did I do that with the ()? Fixed, added the eval, but still nothing. Tried this too and still nothing: - function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = callback;
-
http.send(null);
-
}
-
-
function getRatecard() {
-
url = "Rfunc.php";
-
initAjax(url, function (response) {
-
eval(response.responseText);
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
});
-
}
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.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead Oops, how did I do that with the ()? Fixed, added the eval, but still nothing. Tried this too and still nothing: - function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = callback;
-
http.send(null);
-
}
-
-
function getRatecard() {
-
url = "Rfunc.php";
-
initAjax(url, function (response) {
-
eval(response.responseText);
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
});
-
}
Ok, this is looking pretty good. But now we have a conflict: -
eval(response.responseText);
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
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): - function getRatecard() {
-
url = "Rfunc.php";
-
initAjax(url, function (response) {
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
});
-
}
Quote:
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.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax - function getRatecard() {
-
url = "Rfunc.php";
-
initAjax(url, function (response) {
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
});
-
}
Done, but still nothing from "response.responseText;". Is it possible we need to wait for "http.readyState == 4"? I tried putting that around the "...innerHTML..." line but still nothing. Quote:
Originally Posted by pbmods 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.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead Done, but still nothing from "response.responseText;". Is it possible we need to wait for "http.readyState == 4"? Ooh. Good point. Alrightey. Let's add another layer of complexity to initAjax: -
function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = function(response) {
-
if(response.readyState == 4)
-
callback(response);
-
};
-
http.send(null);
-
}
-
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.onreadystatechange; 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. Quote:
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. -
initAjax(url, function (response) {
-
alert(response.responseText);
-
});
-
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by pbmods Try alerting your responseText to see if your script is sending anything back to you. -
initAjax(url, function (response) {
-
alert(response.responseText);
-
});
-
Tried this: - function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = function(response) {
-
if (response.readyState == 4) { callback(response); }
-
};
-
alert('ack');
-
http.send(null);
-
}
-
-
function getTestpage() {
-
url = "afunc.php";
-
initAjax(url, function (response) {
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
alert(response.responseText);
-
});
-
alert('done');
-
}
I get alerts for "ack" and "done" but not "response.responseText". Not even a blank alert, no alert at all.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead Tried this: - function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = function(response) {
-
if (response.readyState == 4) { callback(response); }
-
};
-
alert('ack');
-
http.send(null);
-
}
-
-
function getTestpage() {
-
url = "afunc.php";
-
initAjax(url, function (response) {
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
alert(response.responseText);
-
});
-
alert('done');
-
}
I get alerts for "ack" and "done" but not "response.responseText". 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?): - function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = function() {
-
if (http.readyState == 4) { callback(http); }
-
};
-
http.send(null);
-
}
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by pbmods 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?): Having fun... like the time our driver's ed instructor hit the car in front when we asked him to show us parallel parking with real cars instead of cones... 8-) - function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = function() {
-
if (http.readyState == 4) { callback(http); }
-
};
-
http.send(null);
-
}
w00t! It lives!
But.... ::sobbing:: ... now there's no focus :-( - function initAjax(url, callback) {
-
var http = getHTTPObject();
-
http.open("GET", url, true);
-
http.onreadystatechange = function() {
-
if (http.readyState == 4) { callback(http); }
-
};
-
http.send(null);
-
}
-
-
function getTestpage() {
-
url = "afunc.php";
-
initAjax(url, function (response) {
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
fldname.focus();
-
});
-
}
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead But.... ::sobbing:: ... now there's no focus :-( Hm. Your code is looking pretty good. The only thing I can think of is that fldname is not defined.
Check your error console and see what's going on.
| | Member | | Join Date: Mar 2006 Location: Ohio
Posts: 56
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by pbmods Hm. Your code is looking pretty good. The only thing I can think of is that fldname is not defined.
Check your error console and see what's going on. This works: - function getTestpage() {
-
url = "afunc.php";
-
initAjax(url, function (response) {
-
document.getElementById(theOutput).innerHTML = response.responseText;
-
document.getElementById('fldname').focus();
-
});
-
}
Whew!
Felt like wandering in the desert for 40 years just to go 20 miles, but worth the trip because things were learned. Thanks again for all your help. :-)
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Focus in Textbox when the page is Loaded through Ajax Quote:
Originally Posted by webhead Felt like wandering in the desert for 40 years just to go 20 miles, but worth the trip because things were learned. On both sides (both parts :P). Quote:
Originally Posted by webhead Thanks again for all your help. :-) You are most welcome. Come back anytime!
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|