Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tạo HTML và in ra Khi bạn tạo một chương trình bằng ngôn ngữ nào, điều đầu tiên bạn muốn xem là đầu ra của chương trình hiển thị trên màn hình. Trong trường hợp của JavaScript, bạn sẽ thấy đầu ra của bạn trong cửa sổ trình duyệt. Tất nhiên, sử dụng trình duyệt HTML xuất ra các định dạng. Mặc dù JavaScript không hiểu HTML cho mỗi gia nhập, nó có thể tạo ra HTML đầu ra với nó được xây dựng trong phương pháp, viết () và writeln () | 2.3 Generating HTML and Printing Output 37 The src attribute is used when the JavaScript code is in an external file the file name ending with a js extension. The src attribute is assigned the name of the file which can be prefixed with its location e.g. a directory tree or URL . script type text javascript src sample.js script script type text javascript src directory sample.js script script type text javascript src http hostname sample.js script 2.3 Generating HTML and Printing Output When you create a program in any language the first thing you want to see is the output of the program displayed on a screen. In the case of JavaScript you ll see your output in the browser window. Of course browsers use HTML to format output. Although JavaScript doesn t understand HTML per se it can generate HTML output with its built-in methods write and writeln . 2.3.1 Strings and String Concatenation A string is a character or set of characters enclosed in matching quotes. Because the methods used to display text take strings as their arguments this is a good time to talk a little about strings. See Chapter 9 JavaScript Core Objects for a more complete discussion. All strings must be placed within a matched set of either single or double quotes for example this is a string or this is a string Double quotes can hide single quotes for example I don t care And single quotes can hide double quotes for example He cried Ahoy Either way the entire string is enclosed in a set of matching quotes. Concatenation is caused when two strings are joined together. The plus sign is used to concatenate strings for example From the Library of WoweBook.Com 38 Chapter 2 Script Setup hot dog or San Francisco br For more information on strings see Chapter 3 The Building Blocks Data Types Literals and Variables. 2.3.2 The write and writeln Methods One of the most important features of client-side JavaScript is its ability to generate pages dynamically. Data text and HTML itself can be written to the .