JS ajax request

Discussion in 'Javascript' started by Igoogler, Dec 6, 2008.

  1. Igoogler

    Igoogler Guest

    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
     
    Igoogler, Dec 6, 2008
    #1
    1. Advertisements

  2. Igoogler

    David Mark Guest

    On Dec 5, 8:55 pm, Igoogler <> wrote:
    > 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.)
     
    David Mark, Dec 6, 2008
    #2
    1. Advertisements

  3. Igoogler

    Bryan A Guest

    On Dec 5, 6:05 pm, David Mark <> wrote:
    > On Dec 5, 8:55 pm, Igoogler <> wrote:
    >
    > > 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
     
    Bryan A, Dec 6, 2008
    #3
  4. David Mark wrote:
    > On Dec 5, 8:55 pm, Igoogler <> wrote:
    >> if (xmlHttp==null) {

    >
    > 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
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #4
  5. Thomas 'PointedEars' Lahn wrote:
    > David Mark wrote:
    >> On Dec 5, 8:55 pm, Igoogler <> wrote:
    >>> if (xmlHttp==null) {

    >> 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)


    The equivalent for the gauntlet pattern is, of course,

    if (!xmlHttp)


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #5
  6. Igoogler

    David Mark Guest

    On Dec 6, 7:23 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > David Mark wrote:
    > > On Dec 5, 8:55 pm, Igoogler <> wrote:
    > >> if (xmlHttp==null) {

    >
    > > Always use strict (===) comparison with null.

    >
    > 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]
     
    David Mark, Dec 6, 2008
    #6
  7. David Mark wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> David Mark wrote:
    >>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    >>>> if (xmlHttp==null) {
    >>> Always use strict (===) comparison with null.

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

    >
    > Clarity.


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

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

    >
    > 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
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #7
  8. Igoogler

    David Mark Guest

    On Dec 6, 1:50 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > David Mark wrote:
    > > Thomas 'PointedEars' Lahn wrote:
    > >> David Mark wrote:
    > >>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    > >>>> if (xmlHttp==null) {
    > >>> Always use strict (===) comparison with null.
    > >> There is no good reason why.  The better piece of advice is:

    >
    > > Clarity.

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


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

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

    >
    > > 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.


    You must have missed this line:

    var xmlHttp=null;

    [snip]
     
    David Mark, Dec 6, 2008
    #8
  9. David Mark wrote/zu Deiner Priorität-Alpha-1-Nachricht von Sternzeit
    06.12.2008 19:55:
    > On Dec 6, 1:50 pm, Thomas 'PointedEars' Lahn <>
    > wrote:
    >> David Mark wrote:
    >>> Thomas 'PointedEars' Lahn wrote:
    >>>> David Mark wrote:
    >>>>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    >>>>>> if (xmlHttp==null) {
    >>>>> Always use strict (===) comparison with null.
    >>>> There is no good reason why. The better piece of advice is:
    >>> Clarity.

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

    >
    > Error-prone?! LOL. I doubt the inefficient claim as well.
    >
    >>>> don't compare against `null' for supposed object references.
    >>> 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.

    >
    > You must have missed this line:
    >
    > var xmlHttp=null;
    >
    > [snip]
     
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #9
  10. David Mark wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> David Mark wrote:
    >>> Thomas 'PointedEars' Lahn wrote:
    >>>> David Mark wrote:
    >>>>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    >>>>>> if (xmlHttp==null) {
    >>>>> Always use strict (===) comparison with null.
    >>>> There is no good reason why. The better piece of advice is:
    >>> Clarity.

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

    >
    > Error-prone?! LOL. I doubt the inefficient claim as well.


    Maybe you want to review the algorithms?

    >>>> don't compare against `null' for supposed object references.
    >>> 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.

    >
    > 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
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #10
  11. Igoogler

    David Mark Guest

    On Dec 6, 2:55 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > David Mark wrote:
    > > Thomas 'PointedEars' Lahn wrote:
    > >> David Mark wrote:
    > >>> Thomas 'PointedEars' Lahn wrote:
    > >>>> David Mark wrote:
    > >>>>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    > >>>>>> if (xmlHttp==null) {
    > >>>>> Always use strict (===) comparison with null.
    > >>>> There is no good reason why.  The better piece of advice is:
    > >>> Clarity.
    > >> Maybe so, but needlessly inefficient, error-prone clarity at that.

    >
    > > Error-prone?!  LOL.  I doubt the inefficient claim as well.

    >
    > Maybe you want to review the algorithms?


    Maybe not. I am surprised you did not take the opportunity.

    >
    > >>>> don't compare against `null' for supposed object references.
    > >>> Yes, it would be better not to assign anything to the variable
    > >>> initially.
    > >> That's not what I meant.  Obviously no previous initialization takesplace here.

    >
    > > 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.


    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]
     
    David Mark, Dec 6, 2008
    #11
  12. David Mark wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> David Mark wrote:
    >>> Thomas 'PointedEars' Lahn wrote:
    >>>> David Mark wrote:
    >>>>> Thomas 'PointedEars' Lahn wrote:
    >>>>>> David Mark wrote:
    >>>>>>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    >>>>>>>> if (xmlHttp==null) {
    >>>>>>> Always use strict (===) comparison with null.
    >>>>>> There is no good reason why. The better piece of advice is:
    >>>>> Clarity.
    >>>> Maybe so, but needlessly inefficient, error-prone clarity at that.
    >>> Error-prone?! LOL. I doubt the inefficient claim as well.

    >> Maybe you want to review the algorithms?

    >
    > Maybe not. I am surprised you did not take the opportunity.


    I didn't want to spoil the experience.

    >>>>>> don't compare against `null' for supposed object references.
    >>>>> 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.
    >>> 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.

    >
    > 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.

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

    >
    > 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
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #12
  13. Igoogler

    David Mark Guest

    On Dec 6, 5:09 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > David Mark wrote:
    > > Thomas 'PointedEars' Lahn wrote:
    > >> David Mark wrote:
    > >>> Thomas 'PointedEars' Lahn wrote:
    > >>>> David Mark wrote:
    > >>>>> Thomas 'PointedEars' Lahn wrote:
    > >>>>>> David Mark wrote:
    > >>>>>>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    > >>>>>>>> if (xmlHttp==null) {
    > >>>>>>> Always use strict (===) comparison with null.
    > >>>>>> There is no good reason why.  The better piece of advice is:
    > >>>>> Clarity.
    > >>>> Maybe so, but needlessly inefficient, error-prone clarity at that.
    > >>> Error-prone?!  LOL.  I doubt the inefficient claim as well.
    > >> Maybe you want to review the algorithms?

    >
    > > Maybe not.  I am surprised you did not take the opportunity.

    >
    > I didn't want to spoil the experience.
    >
    > >>>>>> don't compare against `null' for supposed object references.
    > >>>>> 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.
    > >>> 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.

    >
    > > 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.
    >
    > >>   var xmlHttp

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

    >
    > > 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.
    >

    That was Point #1. And then there was Point #2 which questioned the
    need to "initialize" it in the first place.
     
    David Mark, Dec 6, 2008
    #13
  14. Igoogler

    dhtml Guest

    David Mark wrote:
    > On Dec 6, 1:50 pm, Thomas 'PointedEars' Lahn <>
    > wrote:
    >> David Mark wrote:
    >>> Thomas 'PointedEars' Lahn wrote:
    >>>> David Mark wrote:
    >>>>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    >>>>>> if (xmlHttp==null) {
    >>>>> Always use strict (===) comparison with null.
    >>>> There is no good reason why. The better piece of advice is:
    >>> Clarity.

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

    >
    > 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

    --
    comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
     
    dhtml, Dec 6, 2008
    #14
  15. Igoogler

    David Mark Guest

    On Dec 6, 5:55 pm, dhtml <> wrote:
    > David Mark wrote:
    > > On Dec 6, 1:50 pm, Thomas 'PointedEars' Lahn <>
    > > wrote:
    > >> David Mark wrote:
    > >>> Thomas 'PointedEars' Lahn wrote:
    > >>>> David Mark wrote:
    > >>>>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    > >>>>>> if (xmlHttp==null) {
    > >>>>> Always use strict (===) comparison with null.
    > >>>> There is no good reason why.  The better piece of advice is:
    > >>> Clarity.
    > >> Maybe so, but needlessly inefficient, error-prone clarity at that.

    >
    > > 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.


    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]
     
    David Mark, Dec 6, 2008
    #15
  16. dhtml wrote:
    > David Mark wrote:
    >> Thomas 'PointedEars' Lahn wrote:
    >>> David Mark wrote:
    >>>> Thomas 'PointedEars' Lahn wrote:
    >>>>> David Mark wrote:
    >>>>>> On Dec 5, 8:55 pm, Igoogler <> wrote:
    >>>>>>> if (xmlHttp==null) {
    >>>>>> Always use strict (===) comparison with null.
    >>>>> There is no good reason why. The better piece of advice is:
    >>>> Clarity.
    >>> Maybe so, but needlessly inefficient, error-prone clarity at that.

    >> Error-prone?! LOL. I doubt the inefficient claim as well.

    >
    > 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
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Dec 7, 2008
    #16
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Birtle
    Replies:
    2
    Views:
    2,259
    John Saunders
    Oct 16, 2003
  2. Replies:
    20
    Views:
    1,979
    d1srupt3r
    Mar 31, 2008
  3. darrel

    beginner AJAX questions (AJAX.net)

    darrel, Apr 27, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    2,501
    Flinky Wisty Pomm
    Apr 28, 2006
  4. jobs
    Replies:
    0
    Views:
    397
  5. minnie
    Replies:
    1
    Views:
    800
    Andrew Thompson
    Dec 13, 2006
Loading...

Share This Page