SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    0 Post(s)
    0 Thread(s)

    Best Practices - Unobtrusive Javascript?

    I was going through some of my older designs earlier today, I decided to go ahead and separate my js event handlers from my html markup. It took me about 4 hours but I got most of it done, then I moved to my js file and noticed there was some code that was in place to create html markup on the fly. I noticed this code was attaching event handlers much in the same way I had been doing in my html markup. By that I mean "event='js code;'". I found myself asking the question should I be separating the javascript much the same way I did in my html file.

    So here's my question is it ok to attach event handlers to my html markup using inline js ("event='js code;'") or does it even matter being that the code is contained within a external .js file and as such is being created by javascript?

    To follow up is it best to create elaborate* xhtml markup using createElement or can I get away with using innerHTML?

    Like the title says i'm looking for best practices and what better conforms the newer standards.

    * Markup consisting of 7 or more html tags
    PHP MySQL Shopping Cart

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Sydney, NSW, Australia
    25 Post(s)
    1 Thread(s)
    With XHTML you should really be using createElementNS() to create the elements in the appropriate namespace. Neither createElement or innerHTML work with XHTML, they only work with HTML. With HTML createElement would be best practice since you would be following the standard method rather than simply one that works in all the popular browsers and so hopefully your code would even work in obscure browsers that follow the standards but don't support innerHTML.
    Stephen J Chapman, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts