- Refresher on the Different Types of Scripting
- Displaying Random Content on the Client Side
- Understanding the Document Object Model
- Using window Objects
- Working with the document Object
- Accessing Browser History
- Working with the location Object
- More About the DOM Structure
- Working with DOM Nodes
- Creating Positionable Elements (Layers)
- Hiding and Showing Objects
- Modifying Text Within a Page
- Adding Text to a Page
- Changing Images Based on User Interaction
- Thinking Ahead to Developing HTML5 Applications
- Summary
- Q & A
- Workshop
Q & A
Q. If I want to use the random-quote script from this lesson, but I want to have a library of a lot of quotes, do I have to put all the quotes in each page?
A. If you’re working entirely on the client side, you can also put these quotes in a separate document and reference it in your code. This method will work as long as each item in the array is present in the browser in some way. However, you can begin to see a bit of a tipping point between something that can be on the client side and something that is better dealt with on the server side. If you have a true library of random quotations and only one is presented at any given time, it’s probably best to store those items in a database table and use a little piece of server-side scripting to connect to that database, retrieve the text, and print it on the page.
Q. Can I avoid assigning an id attribute to every DOM object I want to handle with a script?
A. Yes. Although the scripts in this chapter typically use the id attribute for convenience, you can actually locate any object in the page by using combinations of node properties such as firstChild and nextSibling. However, keep in mind that any change you make to the HTML can change an element’s place in the DOM hierarchy, so the id attribute is a reliable recommended way to handle this.
Q. Can I change history entries or prevent the user from using the Back and Forward buttons?
A. You can’t change the history entries. Additionally, you can’t prevent the use of the Back and Forward buttons, but you can use the location.replace() method to load a series of pages that don’t appear in the history. There are a few tricks for preventing the Back button from working properly, but I don’t recommend them—that’s the sort of thing that gives JavaScript a bad reputation.