80 likes | 182 Vues
This week's practical JavaScript lesson explores where to place your JavaScript code within an HTML document for optimal performance. It highlights popular conventions, such as inserting scripts at the bottom of the document to reduce load times. The lesson also covers sharing JavaScript between HTML documents via external files, the importance of form validation, AJAX concepts for asynchronous communication, and creating rich user interfaces with various interactive elements. Refer to the provided resources for examples and further experimentation.
E N D
JavaScript JS301 Week 3: Practical JavaScript
Where does my JavaScript go? Popular convention says the HTML head: <!DOCTYPE html> <html><head> <title>JS301</title> <script type="text/javascript"> document.write('Hello World!'); </script> </head> <body></body> </html>
Where does my JavaScript go? For best performance, but it as close to the bottom of the document as you can: <!DOCTYPE html> <html> <head><title>JS301</title></head> <body> ... <script type="text/javascript"> document.write('Hello World!'); </script> </body> </html>
Where does my JavaScript go? If you want to share JavaScript between HTML documents, include it from an external file: <script src="myscript.js" type="text/javascript"> </script>
Form Validation • Alert users to problems before submitting to the server • Make sure to also validate on the server - never trust the client! • http://jsfiddle.net/zymsys/enDzA/
AJAX • Interact with the server without page loads • Asynchronous JavaScript and XML • XHR: XMLHTTPRequest • Not limited to XML - I prefer JSON • http://jsfiddle.net/zymsys/sHhsf/
Rich User Interfaces • Dialogs: http://jsfiddle.net/zymsys/HcnRd/ • Tabs: http://jsfiddle.net/zymsys/AcUqr/ • Input Controls: • Autocomplete: http://jsfiddle.net/zymsys/eZNQk/ • Buttons: http://jsfiddle.net/zymsys/HqEng/ • Dates: http://jsfiddle.net/zymsys/vgzGU/ • More: http://jqueryui.com/demos/
Next Week • Knockout? • OOP? • Patterns? • Something else?