Chapter 10. The Browser Environment

You know that JavaScript programs need a host environment. Most of what you learned so far in this book was related to core ECMAScript/JavaScript and can be used in many different host environments. Now, let's shift the focus to the browser as this is the most popular and natural host environment for JavaScript programs. In this chapter, you will learn the following topics:

  • The Browser Object Model (BOM)
  • The Document Object Model (DOM)
  • Browser events
  • The XMLHttpRequest object

Including JavaScript in an HTML page

To include JavaScript in an HTML page, you will need to use the <script> tag as follows:

    <!DOCTYPE> 
    <html> 
      <head> 
        <title>JS test</title> 
        <script src="somefile.js"></script> 
      </head> 
      <body> 
        <script> 
          var a = 1; 
          a++; 
        </script> 
      </body> 
    </html> 

In this example, the first <script> tag includes an external file, somefile.js, which contains JavaScript code. The second <script> tag includes the JavaScript code directly in the HTML code of the page. The browser executes the JavaScript code in the sequence it finds it on the page and all the code in all tags share the same global namespace. This means that when you define a variable in somefile.js, it also exists in the second <script> block.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.140.108