JS ajax request

I

Igoogler

I have a simply request method that gets a random quote from elsewhere
on my server. What I want to do is show an ad every ten requests and
the content the rest of the time. So on the tenth click it would show
the ad where it would normally show the quote. What would I need to
modify:

var xmlHttp
function run_query() {
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("Your browser isn't smart enough for this site");
return;
}
var url="rand.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("quote").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject() {
var xmlHttp=null;
try {
xmlHttp=new XMLHttpRequest();
}catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

Thanks in advance
 
D

David Mark

I have a simply request method that gets a random quote from elsewhere
on my server. What I want to do is show an ad every ten requests and
the content the rest of the time. So on the tenth click it would show
the ad where it would normally show the quote. What would I need to
modify:

var xmlHttp

Missing semi-colon.
function run_query() {
xmlHttp=GetXmlHttpObject();

Is this a constructor function?
if (xmlHttp==null) {

Always use strict (===) comparison with null.
alert ("Your browser isn't smart enough for this site");

Unqualified global reference. Change message to: "My developer wasn't
smart enough to write a proper script."
return;}
Useless.


var url="rand.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("quote").innerHTML=xmlHttp.responseText;

Always test the result of gEBI.
}
}

function GetXmlHttpObject() {

Not a constructor. Start name with lowercase.

[snip]

So why did you not post your "rand.php" to a PHP group? That's what
needs to change (along with the above.)
 
B

Bryan A

I have a simply request method that gets a random quote from elsewhere
on my server. What I want to do is show an ad every ten requests and
the content the rest of the time. So on the tenth click it would show
the ad where it would normally show the quote. What would I need to
modify:
var xmlHttp

Missing semi-colon.
function run_query() {
xmlHttp=GetXmlHttpObject();

Is this a constructor function?
if (xmlHttp==null) {

Always use strict (===) comparison with null.
alert ("Your browser isn't smart enough for this site");

Unqualified global reference.  Change message to: "My developer wasn't
smart enough to write a proper script."
return;}
Useless.



var url="rand.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("quote").innerHTML=xmlHttp.responseText;

Always test the result of gEBI.


function GetXmlHttpObject() {

Not a constructor.  Start name with lowercase.

[snip]

So why did you not post your "rand.php" to a PHP group?  That's what
needs to change (along with the above.)

Sorry for being negligent on the js. Basically the rand.php returns a
string of plain text that it retrieves from a db. I was originally
going to use an increment in the call but I am now thinking that using
a cookie might be the way to go.

Thanks for your help and critique
 
T

Thomas 'PointedEars' Lahn

David said:
Always use strict (===) comparison with null.

There is no good reason why. The better piece of advice is:
don't compare against `null' for supposed object references.

if (xmlHttp)


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
There is no good reason why. The better piece of advice is:
don't compare against `null' for supposed object references.

if (xmlHttp)

The equivalent for the gauntlet pattern is, of course,

if (!xmlHttp)


PointedEars
 
D

David Mark

There is no good reason why.  The better piece of advice is:
Clarity.

don't compare against `null' for supposed object references.

Yes, it would be better not to assign anything to the variable
initially.

[snip]
 
T

Thomas 'PointedEars' Lahn

David said:

Maybe so, but needlessly inefficient, error-prone clarity at that.
Yes, it would be better not to assign anything to the variable
initially.

That's not what I meant. Obviously no previous initialization takes place here.


PointedEars
 
D

David Mark

Maybe so, but needlessly inefficient, error-prone clarity at that.

Error-prone?! LOL. I doubt the inefficient claim as well.
That's not what I meant.  Obviously no previous initialization takes place here.

You must have missed this line:

var xmlHttp=null;

[snip]
 
T

Thomas 'PointedEars' Lahn

David Mark wrote/zu Deiner Priorität-Alpha-1-Nachricht von Sternzeit
06.12.2008 19:55:
Maybe so, but needlessly inefficient, error-prone clarity at that.

Error-prone?! LOL. I doubt the inefficient claim as well.
That's not what I meant. Obviously no previous initialization takes place here.

You must have missed this line:

var xmlHttp=null;

[snip]
 
T

Thomas 'PointedEars' Lahn

David said:
Error-prone?! LOL. I doubt the inefficient claim as well.

Maybe you want to review the algorithms?
You must have missed this line:

var xmlHttp=null;

That line is a good local declaration and initialization
in GetXmlHttpObject(), which is but irrelevant here.

It is the variable being assigned to in run_query() --

xmlHttp=GetXmlHttpObject();

-- that is not initialized before,

var xmlHttp

And its initial value (`undefined') is a false-value as is `null'.


PointedEars
 
D

David Mark

Maybe you want to review the algorithms?

Maybe not. I am surprised you did not take the opportunity.
That line is a good local declaration and initialization
in GetXmlHttpObject(), which is but irrelevant here.

What is good about it? The whole point is that it could have been:

var xmlHttp;
It is the variable being assigned to in run_query() --

  xmlHttp=GetXmlHttpObject();

Which is the above. See the return at the end of the function.
-- that is not initialized before,

And why would it be?
  var xmlHttp

And its initial value (`undefined') is a false-value as is `null'.

Exactly. That's my point.

[snip]
 
T

Thomas 'PointedEars' Lahn

David said:
Maybe not. I am surprised you did not take the opportunity.

I didn't want to spoil the experience.
What is good about it? The whole point is that it could have been:

var xmlHttp;

That's a possibility. However, I find it a good idea to initialize
variables with values which types correspond with their later values.
YMMV.
Exactly. That's my point.

Your point was to compare strictly against `null' instead. Seems
a bit contradictory when you allow `undefined' in place of `null',
where your test would fail.


PointedEars
 
D

David Mark

I didn't want to spoil the experience.




That's a possibility.  However, I find it a good idea to initialize
variables with values which types correspond with their later values.
YMMV.



Your point was to compare strictly against `null' instead.  Seems
a bit contradictory when you allow `undefined' in place of `null',
where your test would fail.
That was Point #1. And then there was Point #2 which questioned the
need to "initialize" it in the first place.
 
D

dhtml

David said:
Error-prone?! LOL. I doubt the inefficient claim as well.

It sounds as if Thomas is claiming that == is more efficient than ===.

That should probably be false for most values. Operator == will perform
conversion. When the === is used, there is no type conversion, and the
algorithm will exit sooner, however, but not by any amount that would
matter.

Possibly a needlessly vehement blunder on part of Thomas.

A case for using == with null is when a parameter variable could be null
or undefined.

Garrett
 
D

David Mark

It sounds as if Thomas is claiming that == is more efficient than ===.

That should probably be false for most values. Operator == will perform
conversion. When the === is used, there is no type conversion, and the
algorithm will exit sooner, however, but not by any amount that would
matter.

That was my assumption.
Possibly a needlessly vehement blunder on part of Thomas.

I can't imagine.
A case for using == with null is when a parameter variable could be null
or undefined.

Right. I prefer type conversion in that case.

[snip]
 
T

Thomas 'PointedEars' Lahn

dhtml said:
It sounds as if Thomas is claiming that == is more efficient than ===.

I'm claiming instead that simple type-conversion to boolean as performed
by the algorithm of the `if' statement is more efficient than any of `==' or
`===', and the ECMAScript Specification, sections 12.5, 11.9.1, and 11.9.5,
say I'm right.

To compare against a specific value, strict or loose, is also more
error-prone than simply type conversion here because nothing says what the
[[Get]] method for a host object must return. What can be assumed, however,
is that the return value is a true-value when successful, and a false-value
otherwise.
[...]
Possibly a needlessly vehement blunder on part of Thomas.

Or you are unable to extract meaning from text by understanding it in the
context in which it was made.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top