SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is an Image a Node in Safari?

    This code:

    Code:
    var img = new Image();
    img.src = 'path/logo.png';
    alert(img.nodeName + ', ' + img.nodeType);
    Produces and alert with "IMG, 1" except in Safari, where I get "undefined, undefined". Subsequent use of this object in an appendChild halts the script in Safari (not altogether unexpected at this point.)

    Any thoughts? My goal is to add an image element to a div.

  2. #2
    SitePoint Addict NikoB's Avatar
    Join Date
    Nov 2005
    Location
    Austria
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe you should use
    Code:
    var img = document.createElement("img");
    img.setAttribute("src", "pat/logo.oong");
    instead...
    Corinis OpenSource Community & Content Management
    http://www.corinis.org

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - that looks hopeful. It doesn't break PC browsers. Unfortunately I won't have access to a Mac until I go into the office on Friday. I'll report my results then.

  4. #4
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not even sure why "img = new Image();" would create a DOM node in any browser... that should be an Image Object not an Image DOM Node.

    NikoB's suggestion should work fine in Safari. There are a few weird bugs in safari JS, but as long as you follow JS standards, you should not have that many issues in Safari 1.3+... it's when you start doing stuff that doesn't follow the Javascript spec that you run into tons of issues

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    I'm not even sure why "img = new Image();" would create a DOM node in any browser... that should be an Image Object not an Image DOM Node.

    NikoB's suggestion should work fine in Safari. There are a few weird bugs in safari JS, but as long as you follow JS standards, you should not have that many issues in Safari 1.3+... it's when you start doing stuff that doesn't follow the Javascript spec that you run into tons of issues
    Yeah, I know just enough to get into trouble.

    This does work in Safari, I just checked.

    However, in my own defense, O'Reilly's JavaScript Pocket Reference clearly says that Image inherits from Element. If the book is right, then I'm thinking Safari's behavior is incorrect.

    But I can see that createElement is the proper way to do it, and I should have thought of it since earlier in the code I had used the method to create several divs.


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
  •