Home > Articles

  • Print
  • + Share This
This chapter is from the book

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.

  • + Share This
  • 🔖 Save To Your Account