- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Best JavaScript Coding Practices
What Is JavaScript?
JavaScript is a language originally developed by the creators of Netscape (now Mozilla) to add website functionality without having to run additional programs like Java Applets or Common Gateway Interface programs.
JavaScript is used in some PDF documents, desktop widgets like time sheet apps and browsers limited to a private intranet. However, its most common use is when programmers create a java script website.
If you want to know how to JavaScript, there are many good books and websites telling you how. This article addresses recommended coding practices when working with JavaScript.
JavaScript is not Java, though the names are similar. Java is a programming language that was developed by Sun Microsystems. JavaScript is not used to create software applications.
Simpler is Better
Write your code for simplicity and speed. Review your website periodically and remove all redundant code. Fancy websites that take forever to load will lose potential visitors. Complicated features that fail to run correctly will hurt your image with viewers. Implement JavaScript where necessary and with only the features you need.
Minimize reliance on third party content to avoid problems such as file not found errors or mal-ware delivered by third party apps. JavaScript uses more bandwidth than other scripting library frameworks.
Be careful of using multiple JavaScript apps to gain functionality while slowing down the website. Pick and choose the apps you need, and avoid relying exclusively on the JavaScript libraries for website functionality.
Javascript Best Practices for Security
JavaScript frameworks have been written to integrate with other software languages like CSS. JavaScript is sometimes used to alter how code runs within a web browser contrary to the browser's standard functionality. JavaScript web exploits are periodically identified as a security flaw along with Java applets. Review the security of your website and all code pulled from JavaScript libraries. You can reduce the security risk by running JavaScript scripts in a sandbox and verifying that they are safe before putting them on your website.
Enforce the same-origin policy on all JavaScript methods to block malicious code that could run through security holes. Only use trusted external scripts if you need an external script. While trusted external scripts are read by the website as meeting the same origin as the website, only using trusted external scripts reduces the risk that your new code will create a security hole. Because Java and JavaScript scripts can be reversed and hijacked, you should not use either for security functions like user authentication.
Design Your Website for All of Your Users
Not all of your website's visitors will be humans. Your website will also be visited by search engines and RSS readers. Design your website to work with search engine spiders and syndication feeds.
Javascript programming uses the MVC (model, view, controller) framework. Because of the security risk the use of JavaScript libraries for JavaScript programming introduces, some users choose to have JavaScript disabled. Ensure that your website will work when JavaScript is disabled.
If possible, write your HTML pages for HTML only while making the JavaScript functionality optional. When your developer is creating your website, determine if it possible to use just one site for both traditional PC users and mobile users.
Regular Testing Helps You Make the Grade
Regularly test your JavaScript on new browser versions and new Operating Systems. When you find problems with a new release, fix it as quickly as possible. You may want to become a Beta tester of new JavaScript versions, browsers and operating systems so that you discover and fix problems with your JavaScript code before a production roll-out of the new product.
Always test new Javascript code on a “sandbox” or test system for functionality and security before putting it on your web server.
Keep It Consistent
Adopt and enforce a consistent file naming convention and file structure. Use consistent layouts throughout your website. Regularly clean out unused files and folders while eliminating their use within the website. For example, if you remove a website, ensure that all references to it are removed as well. If you delete a video file, review your references to ensure that they are either deleted or updated to the replacement file.
Check your website for dead links both to internal web pages and external pages every few months, and do the same to make sure your JavaScript references to files on the server are still working. You can maintain archived web pages to prevent the dreaded "page not found" error.
Universal Design
Design your website to run on all major browsers and devices. Different browsers handle JavaScript differently, and mobile devices won't have the same resources as PCs. Try to design your website to work well on all of them. Avoid proprietary Java script code. Search engines will struggle with it, and your website may not work if proprietary technology is unavailable.