Danh mục

Creating Applications with Mozilla-Chapter 5. Scripting Mozilla- P1

Số trang: 21      Loại file: pdf      Dung lượng: 62.22 KB      Lượt xem: 16      Lượt tải: 0    
Hoai.2512

Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu creating applications with mozilla-chapter 5. scripting mozilla- p1, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Creating Applications with Mozilla-Chapter 5. Scripting Mozilla- P1 Chapter 5. Scripting Mozilla- P1In Mozilla, scripting plays important roles in the XPFE. Whether developersrefer to script access and security, user interface logic, XPCOM objectinvocation, or script execution in element event handlers, scripting is sointegral to application development that Mozilla, as a development platform,would be inconceivable without it.The core scripting language used in Mozilla is JavaScript. Although it hashad a reputation as an unsophisticated language used mostly in web pages,JavaScript is more like a first-tier programming language. Modularity, goodexception handing, regular expression enhancement, and number formattingare just some features of the new JavaScript 1.5,[1] which is based on theECMA-262 standard.[2] JavaScript 2.0, due sometime late in 2002, promisesto be an even bigger promotion of the language.Three distinct levels of JavaScript are identified in this chapter. A userinterface level manipulates content through the DOM, a client layer calls onthe services provided by XPCOM, and, finally, an application layer isavailable in which JavaScript can create an XPCOM component. Thefollowing section describes these levels in detail.5.1. Faces of JavaScript in MozillaAs you have already seen in some examples in this book, the user interfaceuses JavaScript extensively to create behavior and to glue various widgetstogether into a coherent whole. When you add code to the event handler ofone element to manipulate another -- for example, when you update thevalue of a textbox using a XUL button -- you take advantage of this firstlevel of scriptability. In this role, JavaScript uses the Document ObjectModel (DOM) to access parts of the user interface as a hierarchicalcollection of objects. The section Section 5.3, later in this chapter, discussesthis highest level of scripting.At a second level, JavaScript glues the entire user interface to the XPCOMlibraries beneath, which create the application core. At this level, XPConnect(see the section Section 5.4.1 later in this chapter) provides a bridge thatmakes these components scriptable, which means that they can be invokedfrom JavaScript and used from the user interface layer. When JavaScriptcalls methods and gets data from scriptable components, it uses this secondlayer of scriptability.Finally, at the third and ultimate level of Mozilla scripting, JavaScript can beused as a first-order language for creating the application core itself, forwriting software components or libraries whose services are called. Wediscuss this third level of scripting and provide a long example in the sectionSection 8.2.1 in Chapter 8.When you use JavaScript in these contexts, the application architecture lookssomething like Figure 5-1, in which scripting binds the user interface to theapplication core through XPConnect and can reside as a software componentusing such technologies as XPIDL and XPCOM.Figure 5-1. Scripting in MozillaNotes[1] This book does not pretend to give a complete overview of JavaScript. You can view the full JavaScript 1.5 reference online at http://developer.netscape.com/docs/manuals/index.html?content=javascript.html.[2] The third edition of the EMCA-262 EMCAScript Language Specification can be found at http://www.ecma.ch/ecma1/STAND/ECMA-262.HTM.5.2. JavaScript and the DOMIn the application layer of Mozilla, there is little distinction between a webpage and the graphical user interface. Mozillas implementation of the DOMis fundamentally the same for both XUL and HTML. In both cases, statechanges and events are propagated through various DOM calls, meaning thatthe UI itself is content -- not unlike that of a web page. In applicationdevelopment, where the difference between application chrome andrendered content is typically big, this uniformity is a significant stepforward.5.2.1. What Is the DOM?The DOM is an API used to access HTML and XML documents. It does twothings for web developers: provides a structural representation of thedocument and defines the way the structure should be accessed from script.In the Mozilla XPFE framework, this functionality allows you to manipulatethe user interface as a structured group of nodes, create new UI and content,and remove elements as needed.Because it is designed to access arbitrary HTML and XML, the DOMapplies not only to XUL, but also to MathML, SVG, and other XMLmarkup. By connecting web pages and XML documents to scripts orprogramming languages, the DOM is not a particular application, product, orproprietary ordering of web pages. Rather, it is an API -- an interface thatvendors must implement if their products are to conform to the W3C DOMstandard. Mozillas commitment to standards ensures that its applicationsand tools do just that.When you use JavaScript to create new elements in an HTML file or changethe attributes of a XUL button, you access an object model in which thesestructures are organized. This model is the DOM for that document or data.The DOM provides a context for the scripting language to operate in. Thespecific context for web and XML documents -- the top-level windowobject, the elements that make up a web document, and the data stored inthose elements as children -- is standardized in several differentspecifications, the most recent of which is the upcoming DOM Level 3standard.5.2.2. The DOM Standards and MozillaThe DOM specifications are split into different levels overseen by the W3C.Each level provides its own features and Mozilla has varying, but nearlycomplete, levels of support for each. Currently, Mozillas support for theDOM can be summarized as follows: • DOM Level 1: Excellent • DOM Level 2: Good • DOM Level 3: Poor; under constructionMozilla strives to be standards-compliant, but typically rea ...

Tài liệu được xem nhiều: