SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot tristanm's Avatar
    Join Date
    Jan 2005
    Location
    Hampshire, UK
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setAttribute() in Opera 7.54 Win

    I'm in the process of migrating a script to use DOM core methods but I've hit a roadblock in the aforementioned browser. The following code snippet illustrates what I'm basically trying to do, it really is incredibly basic!

    HTML Code:
    var newimg = document.createElement('img');
    newimg.setAttribute('id', 'placeholder');
    newimg.setAttribute('src', 'images/large/courtyard.jpg');
    I'm then positioning the element on the page using appendChild(). Everything works perfectly on Firefox 1, IE5+, Opera 8, Konqueror 3.3.1 (Linux) but not in Opera 7.54. I suspect the element is being created as associated styling on #placeholder is being applied (just some padding and borders) but the image itself is not being displayed. I assume therefore that the problems lies with setAttribute()?

    This seems like the sort of problem that others would have encountered, but I haven't been able to find mention of it anywhere.

    Thanks in advance for any and all help.

    Tristan

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't usually bother using setAttribute(), since I can do this.

    Code:
    var newimg = document.createElement('img');
    newimg.id = 'placeholder';
    newimg.src = 'images/large/courtyard.jpg';
    If you are using a custom DTD and want to set the value of a custom attribute some browsers require that setAttribute() be used.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Zealot tristanm's Avatar
    Join Date
    Jan 2005
    Location
    Hampshire, UK
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var newimg = document.createElement('img');
    newimg.id = 'placeholder';
    newimg.src = 'images/large/courtyard.jpg';
    Tried using dot notation and that didn't work either. The dynamically created image seems to refuse any of it's properties being set. I experience the same problem attempting to set the alt attribute via setAttribute and dot notation.

    I haven't yet tried recreating the problem in a simpler page. It may be that something else in this particular page is causing problems.

  4. #4
    SitePoint Zealot tristanm's Avatar
    Join Date
    Jan 2005
    Location
    Hampshire, UK
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, it appears to have something to do with the mime type used. If I serve the page as text/html it all works ok, but serve it properly as application/xhtml+xml and it suddenly stops working. So its clearly got something to do with the XML DOM in Opera.

    Any ideas?

    Thanks

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah! It must be the CDATA problem.

    Put the JS in an external file.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    SitePoint Zealot tristanm's Avatar
    Join Date
    Jan 2005
    Location
    Hampshire, UK
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz
    Ah! It must be the CDATA problem.

    Put the JS in an external file.
    Way ahead of you on that one, I nearly always use external .js files (as I have in this case) and I'm well aware of the SGML/XML commenting issue. Besides, Gecko based browsers (which understand the proper XHTML mime type) have no problem with the script. I suspect it's got something to do with the way the DOM is presented via XML in Opera that's causing the problem. I'm using content negotation on the site so I'm serving Opera text/html for the time being and everything works perfectly. Its far from ideal, but it'll do until I can work out what the problem is; or until Opera 8 is released which doesn't have the same problem

  7. #7
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ever heard of XML namespaces? If you are using application/xhtml+xml, you also need to use createElementNS. Opera removed this requirement in 8.0 despite the fact that it was the correct behaviour.

  8. #8
    SitePoint Zealot tristanm's Avatar
    Join Date
    Jan 2005
    Location
    Hampshire, UK
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Saturn
    Ever heard of XML namespaces? If you are using application/xhtml+xml, you also need to use createElementNS. Opera removed this requirement in 8.0 despite the fact that it was the correct behaviour.
    I'm familiar with the level 2 namespace aware methods yes, but if I'm reading this correctly, Opera 7.x doesn't support them. I understood that the namespace aware methods were required if you are using more than one namespace in a document. In the absence of multiple namespaces, surely the created elements just inherit the namespace specified in the root element <html>? The absence of namespace aware methods doesn't explain why these scripts work perfectly in Gecko based browsers either. It doesn't really explain why the scripts partially work in Opera 7 come to that.

    Having said all that, I'm prepared to give it a go, so I'll let you know how I get on.


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
  •