SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Image()

    Basically what I'm trying to do is get an image to display using JavaScript. I'm using the code below and of course it's not working. I have no idea why, but all that is displayed is the location of where the image is. I've tried different variations of this code with no luck. Can anyone help out? Thanks!

    <script type="text/javascript">
    function car (image) {
    var image = new Image();
    image.src = "ta.jpg";
    document.write(image.src);
    }
    </script>

    <body onLoad="car()">

    </body>


    FYI.. I'm using a JavaScript page and the file is saved as .htm, this is why there's no header or html tags.

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to insert the actual image object. i.e.

    Code:
    var image = new Image();
    
    image.onload = function()
    {
        document.body.appendChild(image);
    }
    
    image.src='someimage.jpg';

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No need for the onload function if you don't want to wait for the image to load before displaying. i.e.

    Code:
    var image = new Image();
    image.src='someimage.jpg';
    document.body.appendChild(image);
    should work as well...

  4. #4
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay. I'll give this a try! Thanks!

  5. #5
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked like a charm! Thank you again!


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
  •