SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to response and image instead of responseText?

    Hello, an http request is calling another page called test.php. And putting it all in an id called "countposts." I'm trying to pull an image from that page. So basically I'm just trying to call whats in the page, not any text at all. But I think there is something missing in the getelement by id, like a src. command or something. Anyone know how to take an image from a php page and stick it in an id? Whats missing in this get element so it puts an image in this id name? Please let me know, thank you very much.

    function handleResponse() {

    if(http.readyState == 4 && http.status == 200){
    document.getElementById("countPosts")
    setTimeout('countPosts()',2000);
    }
    Last edited by 1Jen; May 7, 2007 at 04:24.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    You don't need Ajax to pull an image, you just need to load the image directly.

    var img = new Image();
    img.src = 'imagetoload.gif';
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do because I'm going to have active content going all the time and need it without a page refresh. So how to take an image from test.php and put it in the id? I already have the request and body figured out, just need help with this javascript. Thanks.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The javascript I gave you will retrieve an image from the server. Then you just need to do the appropriate call to add that image into the page. No page refresh required as you can retrieve images from the server whenever you want just by referencing them from Javascript.

    You can't retrieve images using Ajax because the images are not text or XML and are not actually in the web page but are separate files that are referenced from the page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK thank you very much. From looking at the code it looks like it is asking for a gif address. How do I keep the image reloading so that the image changes every so often?
    img.src = 'imagetoload.gif';

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    set up an array of image filenames and then create a function that extracts one of the names from the array. Call the function from a setInterval call that specifies how often you want to download a new image.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much I'll check it out. Is this affected by people who turn off their javascript? Someone please get back to me on how do you turn off javascript anyway? I know how to turn off popups.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    People without JavaScript or with javaScript disabled would have to manually reload the page to get different images.

    How to disable JavaScript is different for different browsers. My page http://javascript.about.com/library/bldisable1.htm has step by step instructions for several different browsers.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great page, thanks a lot felgall I wanted to see if most browsers by default have it enabled and they do. Thanks!!


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
  •