SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Is it a mandate to append an element created by javascript to a parent node

    This may seem silly question but I seek clarification since my application is very huge.

    I have:
    Code:
    var audio = document.createElement("audio");
    Do I have to always append this element to a parent node like:
    Code:
    document.body.appendChild(audio); // or something
    Is it necessary since I remove and create the audio element on the fly base on a handler function or user interaction?

    My current practice is that I never append them to anything. Is this a good practice?

    Note: I do this because there is no current browser supports switching audio API source node natively. It can be done through hack.

    Thank you,

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    You have to append your code somewhere in the DOM, otherwise it will only live in the variable in Javascript and not be accessible by the browser's rendering engine.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much. This is very clear answer. I've also noticed that changing practice in JavaScript bring a significant different performance. That's why I ask.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Force Flow View Post
    You have to append your code somewhere in the DOM, otherwise it will only live in the variable in Javascript and not be accessible by the browser's rendering engine.
    Not entirely correct. You can also insert it. But yes, it needs to go into the DOM somewhere.

  5. #5
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you Mittineague. I do some research base on your suggestion. The bellow link to a (rather old) article suggests that the insert method you recommended is better a practice than appenChild.

    http://www.stevesouders.com/blog/201...-insertbefore/

    You've really made my day.


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
  •