The more I have been getting into using JavaScript the more I have seen the benefits of using Behavioural Separation. The concept is simple, separate your behaviour from your structure/content just as you do your style (spaghetti code is bad people!).
Although at first it seems much harder to use the DOM to apply listeners to buttons and links, after a bit more development you have the fantastic advantage of having all your code in one place and the ability to re-use it across your site.
This has lead me onto using Hijax, Jeremy Keith's buzz word for graceful degradation of Javascript and of-course AJAX. The concept is simple:
- First, build an old-fashioned website that uses hyperlinks and forms to pass information to the server. The server returns whole new pages with each request.
- Now, use JavaScript to intercept those links and form submissions and pass the information via XMLHttpRequest instead. You can then select which parts of the page need to be updated instead of updating the whole page.
You now have an application that can't fail. If you have a user with a browser that can't use JavaScript (a mobile phone for example) or a user that has turned it off (because JavaScript is often used to harass users) the site performs normally. If they have a compatible browser they get super buzz worded version of your site. You also have the added benefit that any bugs in your JavaScript won't fail your site as if the JavaScript stops working so does the " return: false; " on your links meaning they are followed as normal.
The result is a super stable web app, that is extremely easy to manage and maintain.
For more information on Hijax, click here, and click here for more information on Behavioural Separation. You can also find out more about Jeremy Keith and his book, DOM Scripting here