SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Thread: Multiple Ajax's

Hybrid View

  1. #1
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Ajax's

    Hi, say I have:
    Code:
    <script type="text/javascript">
    function ajaxFunction()
    {
    var xmlHttp;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        try
          {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        catch (e)
          {
          alert("Your browser does not support AJAX!");
          return false;
          }
        }
      }
      xmlHttp.onreadystatechange=function()
        {
        if(xmlHttp.readyState==4)
          {
          var data = xmlHttp.responseText.split("\n");
          document.Register.secimg.src=data[0];
          document.Register.sessid.value=data[1];
          }
        }
      xmlHttp.open("GET","scajax.php?r=" + Math.random(),true);
      xmlHttp.send(null);
      }
    </script>
    and I want to use a different Ajax function, would I have to do the same again with a different name (instead of ajaxFunction()) ?

    Thanks

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you say using a different ajax function, do you mean the onreadystatechange function? If so you can just pass a function in as a parameter, then set xmlHttp.onreadystatechange equal to the function argument.

  3. #3
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, just did. Cheers.

  4. #4
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I have a different problem.
    Is it possible to change the onmouseover data of <a> link?

    Code:
    var udata = xmlHttp.responseText.split("\n");
      document.Register.userimg.src=udata[0];
      document.Register.useromo.onmouseover=udata[1];
    It doesn't seem to change, but the image does.

  5. #5
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the value of udata[1]?

  6. #6
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry forgot to mention,
    for testing I have
    PHP Code:
    echo "images/regt.gif";
    echo 
    "\n";
    echo 
    "return overlib('Test.', STICKY, CAPTION, 'Test', CENTER);window.status='Tactical Mafia - Registration';return true;"
    Thanks.

  7. #7
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anything come up in a javascript console of some kind (Firefox's Error Console for example)?

  8. #8
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.Register.useromo has no properties in firebug. I've been out of JS for a while but I had this once and I can't remember how to fix it.

  9. #9
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That error means that document.Register.useromo is not a valid object:

    Code:
    var udata = xmlHttp.responseText.split("\n");
    document.Register.userimg.src=udata[0];
    document.Register.useromo.onmouseover=udata[1];
    The correct name is useromo not userimg right?

  10. #10
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, this is what I have:
    PHP Code:
    <a name="useromo" onmouseover="return overlib('This will be your name (who you are known as) used throughout Tactical Mafia.', STICKY, CAPTION, 'Username', CENTER);window.status='Tactical Mafia - Registration';return true;" onmouseout="nd();window.status='Tactical Mafia';return true;" href="register1.php#" style=""><img src="images/regq.gif" name="userimg" border="0" alt="" /></a
    So useromo is the link.

    I can't figure it out.
    Last edited by TacMaf; Feb 1, 2008 at 15:55.

  11. #11
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can <a> tags even be changed? I tried changing a text input field and it worked fine?

  12. #12
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I got rid of the <a> tags and now I'm just using the image. The .src changing works but the .onmouseover doesnt.

  13. #13
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you can set the onmouseover property to a string in code. A string is used for HTML attributes but a function must be used in code.

    Maybe something like:
    Code:
    document.Register.useromo.onmouseover=new Function(udata[1]);
    You could also try giving your <a> element an id attribute of "useromo" and use
    Code:
    document.getElementById("useromo").onmouseover = new Function(udata[1]);

  14. #14
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah I see. So what kind of thing would I need in the function?

    Thanks very much.

  15. #15
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does udata[1] not contain a function returned as a string from php code?

  16. #16
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    udata[1] is just "Test" set in the ajax script called using GET.

  17. #17
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have this JavaScript:
    Code:
    var udata = xmlHttp.responseText.split("\n");
    and this from php:
    Code:
    echo "images/regt.gif";
    echo "\n";
    echo "return overlib('Test.', STICKY, CAPTION, 'Test', CENTER);window.status='Tactical Mafia - Registration';return true;";
    which should make udata[1] the function string above.

  18. #18
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right. I'm not sure if I've done this right but this is what I have:
    Code:
    var udata = xmlHttp.responseText.split("\n");
      document.Register.userimg.src=udata[0];
      document.Register.userimg.onMouseOver=new Function(udata[1]);
    The OMO still doesn't change thought .

    Thanks.

  19. #19
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript is case sensitive.

    Try onmouseover rather than onMouseOver

  20. #20
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked! Thank you r51!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •