SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast Saidin's Avatar
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    writing html using DOM

    I understand this must be a very basic question, and I searched, but can't find the answer.

    I'm playing around with 'AJAX' a bit and found the following code for updating the content on my page.

    Code:
    var result_wrap = document.createElement('div');
    var line = document.createTextNode(result_collection.item(i).firstChild.nodeValue);
    result_wrap.appendChild(line);
    result_div.appendChild(result_wrap);
    This works nicely for text. However when I want to display a complete string with a hyperlink somewhere within it, it outputs that as text. I guess I must use something else instead of createTextNode, but I can't figure out what.

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Location
    DK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    createTextNode can ONLY create text. You have to create a <a>-tag to put in a link. Use 'setAttribute' to add attributes like 'href' etc...

  3. #3
    SitePoint Enthusiast Saidin's Avatar
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you saying that I have to extract the url from the string and create elements for that string and then put the whole thing back together when displaying it? Is there no easier/faster way to display a string that contains html?

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Location
    DK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, W3C DOM isn't easy to work with. I guess that we some day get used to it...

    You could use the non W3C innerHTML. It's widely supported by newer main browsers.

  5. #5
    SitePoint Enthusiast Saidin's Avatar
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmz, great...

    Thanks for the swift replies.

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    innerHTML is not a standard, but sometimes it can really make life easier. The DOM can also be quite slow in widely used antique browsers (IE).

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Location
    DK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    innerHTML is not a standard
    Yes, but sometimes we need to get things done. Then later we must take the time needed to study the W3C way of manipulating the DOM tree.

    Is IE7 slow when manipulating the DOM tree? Or doesn't it work at all? (you could see my thread Code with createElement...)

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that was exactly my point. Its simply faster than using the DOM. And about IE 7, it is still much slower than Firefox and Opera...

    Question is if its faster than IE 6. If you have both then try this out.

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Location
    DK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that was exactly my point. Its simply faster than using the DOM. And about IE 7, it is still much slower than Firefox and Opera...

    Question is if its faster than IE 6. If you have both then try this out.
    That raise a new question: How do you install both IE6 and IE7?

    I have been searching the internet so find a way to do it. Found some solutions that didn't work! IE6 crashes immediately. How did you do it?

  10. #10
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Microsoft recommends this solution to run IE 6 and IE 7 on the same computer. Have fun


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
  •