JavaSCRIPT 论文翻译
更新时间:2023-05-31 20:29:01 阅读量: 实用文档 文档下载
《Beginning JavaScript with DOM Scripting and Ajax,2006:3-6》 作者:Christian Heilmann
Beginning JavaScript with DOM Scripting and Ajax
The Why of JavaScript
In the beginning of the Web, there was HTML and the Common Gateway Interface (CGI). HTML defines the parts of a text document and instructs the user agent (usually the web browser) how to show it—for example, text surrounded by the tags <p></p> becomes a paragraph. Within that paragraph, you might have <h1></h1> tags that define the main page heading. Notice that for most opening tags, there is a corresponding closing tag that begins with </.
HTML has one disadvantage—it has a fixed state. If you want to change something, or use data the visitor entered, you need to make a round-trip to a server. Using a dynamic technology (such as ColdFusion, Ruby on Rails, , PHP, or JSP), you send the information from forms, or from parameters, to a server, which then performs calculations, tests, database lookups, and other such tasks. The application server associated with these technologies then writes an HTML document to show the results, and the resulting HTML document is returned to the browser for viewing. The problem with that is it means every time there is a change, the entire process must be repeated (and the page reloaded). This is cumbersome and slow. It is true that at least the Western world has the benefit of fast Internet connections these days, but displaying a page still means a reload, which could be a slow process that frequently fails. (Ever get an Error 404?)
Some information, such as the results of performing calculations and verifying the information on a form, might not need to come from the server. JavaScript is executed by the user agent (normally a browser) on the visitor’s computer. We call this
client-side code . This could result in fewer trips to the server and faster-running web sites.
What Is JavaScript?
JavaScript started life as LiveScript, but Netscape changed the name—possibly because of the excitement being generated by Java—to JavaScript. The name is confusing, though, because there is no real connection between Java and
JavaScript—although some of the syntax looks similar.
Java is to JavaScript what Car is to Carpet
—From a JavaScript discussion group on Usenet
Netscape created the JavaScript language in 1996 and included it in their Netscape Navigator (NN) 2.0 browser via an interpreter that read and executed the JavaScript added to .html pages. The language has steadily grown in popularity since then, and it is now supported by all browsers and some applications as a way to customize them.
The good news is that this means JavaScript can be used in web pages for all browsers. The not-quite-so-good news is that there are differences in the way the different browsers implement JavaScript, although the core JavaScript language is much the same. However, JavaScript can be turned off by the user. I will discuss this further shortly, as well as throughout this book.
The great thing about JavaScript is that once you’ve learned how to use it for
browser programming, you can move on to use it in other areas. Microsoft Windows 8 and Surface tablets both let you use JavaScript to develop applications, PDF files use JavaScript, and applications such as Dreamweaver and Photoshop are scriptable with JavaScript. Now mobile applications can be developed with JavaScript and converted to native code. JavaScript can even be used on the server side using things like Node.js.
A lot of large companies also offer software development kits (SDKs) that let you access data or integrate services on your site. For example, if you want your visitors to log in with their Facebook ID, you can use Facebook’s JavaScript SDK located at /web/.
Even better is the fact that JavaScript is a lot easier to develop than higher
programming languages or server-side scripting languages. It does not need any compilation like Java or C++, and it doesn’t need to be run on a server or command line like Perl, PHP, or Ruby. All you need to write, execute, debug, and apply JavaScript is a text editor and a browser—both of which are supplied with any operating system. There are, of course, tools that make it a lot easier for you—for example, JavaScript debuggers like Firebug, Opera Dragonfly, and Chrome Developer tools.
Problems and Merits of JavaScript
As mentioned at the outset of this chapter, JavaScript has been an integral part of web development over the last few years, but it has also been used incorrectly. As a result, it has received a bad reputation. The reason for this is the use of gratuitous JavaScript effects, like moving page elements and pop-up windows. These effects might have been impressive the first time you saw them, but they soon become just a “nice to have” feature and, in some cases, even a “nice to not have any longer” element. A lot of this comes from the days of DHTML (which I’ll say more about in Chapter 3). The term user agent and the lack of understanding of what a user agent is can also be a problem. Normally, the user agent is a browser like Microsoft Internet Explorer (IE), Chrome, Firefox (Fx), Opera, or Safari. However, browsers are not the only user agents on the Web. Others include
Assistive technology that helps users to overcome the limitations of a
disability—like text-to-speech software or Braille displays
Text-only agents like Lynx
Web-enabled applications
Browsers in game consoles
Browsers in smartphones
Browsers in tablets
Interactive TV and set-top boxes
Search engines and other indexing programs
And many more
This large variety of user agents, of different levels of technical finesse (and old user agents that don’t get updated), is also a great danger for JavaScript.
Not all visitors to your web site will experience the JavaScript enhancements you applied to it. A lot of them will also have JavaScript turned off—for security reasons. JavaScript can be used for good and for evil. If the operating system—like unpatched Windows—allows you to, you can install viruses or Trojan Horses on a computer via
JavaScript or read out user information and send it to another server.
Note There is no way of knowing what the visitor uses or what his computer is capable of. Furthermore, you never know what the visitor’s experience and ability are. This is one of the beautiful aspects of the
Web—everyone can participate. However, this can introduce a lot of unexpected consequences for the JavaScript programmer.
In many cases, you might want to have a server-side backup plan. It would test to see whether the user agent supports the functionality desired and, if it doesn’t, the server takes over.
Independence of scripting languages is a legal requirement for web sites, as defined in the Digital Discrimination Act for the UK, section 508 in the US law, and many more local legal requirements throughout the world. This means that if the site you developed cannot be used without JavaScript, or your JavaScript enhancements are expecting a certain ability of the users or their user agent without a fallback, your client could be sued for discrimination.
However, JavaScript is not evil or useless, and it is a great tool to help your visitors to surf web sites that are a lot slicker and less time-consuming.
The merits of using JavaScript are
Less server interaction You can validate user input before sending the page
off to the server. This saves server traffic, which means saving money.
Immediate feedback to the visitors They don’t have to wait for a page reload to see if they forgot to enter something
Informing the visitor of minor errors For example, if you need your visitors to fill out a form, JavaScript can provide instant feedback on how the
form was filled out. If a required field is missing, the site can inform the user before submitting any data to the server.
Increased usability by allowing visitors to change and interact with the user interface without reloading the page For example, by collapsing and expanding sections of the page or offering extra options for visitors with JavaScript. A classic example of this is selection boxes that allow immediate filtering, such as only showing the available destinations for a certain airport, without making you reload the page and wait for the result.
Increased interactivity You can create interfaces that react when the user hovers the pointer over them or activates them via the keyboard. This is partly possible with cascading style sheets (CSS) and HTML as well, but JavaScript offers you a lot wider—and more widely supported— range of options.
Richer interfaces If your users allow for it, you can use JavaScript to include such items as drag-and-drop components and sliders—something that originally was possible only in thick client applications your users had to install, such as Java applets or browser plug-ins like Flash.
Lightweight environment Unlike Java applets or Flash movies, which are large files that must be downloaded, scripts are small in file size and get cached (temporary storage) once they have been loaded. JavaScript also uses the browser controls for functionality rather than its own user interfaces like Flash or Java applets do. This makes it easier for users, because they already know these controls and how to use them. Modern Flash and Apache Flex applications do have the option to stream media and—being vector based—are visually scalable, something JavaScript and HTML controls aren’t. On the other hand, SVG (Scalable Vector Graphics) is something native to the browser and can be controlled by JavaScript.
JavaScript DOM与Ajax
1.1 JavaScript产生的原因
在Web发展的初期,主要有HTML和公共管理接口(CGI)。HTML定义了大部分的文本文档并且指示用户代理(通常为网页浏览器)如何显示。比如,标签<p></p>之间的文字就会成为一个段落,在这个段落中可以使用标签<h1></h1>来定义最主要的页面标题。注意大多数开始标签,都会有相应的以</开头的结束标签。
HTML有一个缺点,即他的状态是固定不变的。如果想改变一些东西或者使用用户输入的数据,就需要向服务器提出一个往返的请求。使用动态技术(如ColdFusion、ASP、、PHP或JSP)就可以从表单或者参数中将信息发送到服务器然后通过服务器完成计算、测试、数据库查找等。与这些技术相关联的应用程序服务器则会产生一个HTML文档来显示结果,然后把处理的结果一HTML文档的形式返回到浏览器以供用户查看。
这样做的问题在于无论何时网页只要有变化,以上整个过程都需要再重复执行一遍(并且重新加载网页)。这样会显得比较笨重缓慢,没有网络这个新媒介对我们承诺的那么美好。现在,人们已经普遍拥有了快速的因特网连接。但是显示一个页面仍然意味着重新加载,这是一个时常失败的缓慢过程(遇到过Error 404没有?)。
我们需要更加灵活的东西——要允许Web开发人员快速地对用户给予反馈并且不用从服务器重新加载页面就能改变HTML。可以想象有这么一个表单,只要有一个字段中产生了错误,它都需要重新加载,如果能够不用重新从服务器加载页面,就能快速地获取错误提示,那岂不是更方便实用?这正是JavaScript的用武之地。
一些信息(比如表单上的一些计算和验证信息)并不需要依靠服务器。JavaScript可以由访问者电脑上的用户代理(通常是一个浏览器)来执行。我们把这叫做客户端代码(client-side code)。这样可以减少与服务器的交互成本并且使网站运行得更快。
1.2 JavaScript是什么
JavaScript的前身是LiveScript,但是网景公司后来把名字改成了JavaScript,很可能是由于Java的火爆。这个名字经常会令人感到迷惑,因为尽管Java与JavaScript有些语法比较相近,但它们之间并没有必然的联系。
Java之于JavaScript就好比Car(汽车)之于Carpet(地毯)。
——来自Usenet上的JavaScript讨论组
网景公司在1996年创造了JavaScript语言,它包含在Netscape Navigator(NN)2.0浏览器中,用解释器来读取和执行添加到.html页面的JavaScript代码。从此,这种语言稳步发展壮大并越来越普及,现在大多数浏览器都支持它。
这意味着JavaScript可以用于网页中,被所有现代的浏览器所解释。但是,
不同的浏览器在实现JavaScript的方式上是不同的,尽管核心的JavaScript语言是一样的。不过,JavaScript可以被用户关闭掉,并且一些公司和机构从安全的角度考虑要求他们的用户这样做。这个我们稍后(贯穿本书)会进一步讨论。
关于JavaScript最大的特点就是,一旦学会了如何在浏览器编程中使用它,你就可以把它应用到其他的领域中。微软的服务器使用JavaScript去做服务器端网页编程(ASP),PDF文件现在也在使用JavaScript,甚至Windows的任务管理也可以使用JavaScript来自动运行。许多应用程序,如Dreamweaver和Photoshop,都可以使用JavaScript来编写脚本。操作系统上的许多插件,比如苹果公司的Dashboard或者Linux和Windows平台上的Konfabulator,甚至允许使用JavaScript编写小的帮助程序。
最近许多大公司也提供了可用在网页中的JavaScript对象和方法组成的API(应用编程接口),Google Maps就是其中的一种。只需要使用几行代码就可以在你的网站中提供可缩放和可滚动的地图。
另一个更好地特点就是,JavaScript比高级编程语言和服务器端编程更容易开发。它不需要像Java和C++那样编译,也不需要像Perl、PHP或Ruby语言那样运行在服务器上或需要在命令行执行。编写、执行、调试和应用JavaScript脚本所需要的只是文本编辑器和浏览器,而这两者在所有的操作系统上都提供。当然,也有工具可以使你更加方便,如Mozilla Venkman、Microsoft Script Debugger和kjscmd这样的JavaScript调试器。
1.3 JavaScript的问题和价值
正如我在本章的开始提到的,JavaScript在过去的几年里就已经成为Web开发的一
个完整的部分,但是它也经常被错误地使用。结果,它就落了一个不好的名声。导致这个结果的原因是某些严重影响用户的JavaScript特效,如移动的页面元素和弹出窗口。这种情况你第一次看到印象会很深刻,但很快就变成了“有了也不错”,在有些情况下,甚至变成“没有更好”。许多类似的效果都来自DHTML时代(相见第3章)。
术语用户代理(user agent)和对其含义的缺乏理解同样也是一个问题。通常,用户代理是指一个浏览器,比如微软的IE,Netscape、Mozilla(Moz),Firefox(Fx)、Opera或Safari。但是浏览器不是web上唯一的用户代理,其他用户代理还包括:
辅助技术,用来帮助用户克服它们的缺陷——比如语音合成软件或者盲文显示器。
纯文本代理,例如Lynx。 支持Web的应用程序。 游戏控制台。 手机。 个人数字助理(PDA)。 交互式的电视机顶盒。 搜索引擎和其他索引程序 其他
这么多类的用户代理(以及一些没有更新的旧用户代理),它们使用了不同的技术手段,对于JavaScript也是一个非常大的危险。
并不是所有的访问者都能体验到你所应用的JavaScript增强,许多人都会出于安全的考虑关闭JavaScript。JavaScript既可以用来做好事,也可以用来做坏事。如果操作系统(比如没有打补丁的windows)语序,可以通过JavaScript来在某台计算机上安装病毒或木马,或者读取用户信息并把它发送到另外一个服务器
上。
■注解 没有办法知道访问者在使用什么工具,他的计算机功能如何。再者,你永
远不知道访问者的经验和能力如何。这也是网络很好的一个方面——每个人都可以参与。然而,这样会给JavaScript程序员带来意外的后果。
在大多数情况下,你可能想要一种服务器端的替代方案。它可以测试用户代理是否支持所需要的功能,如果不支持,服务器会使用其替代方案。
脚本语言的独立性对于网站是法律方面的要求,在英国的数字歧视法、美国法律的508条款和世界上其他地区的法律中都有定义。这意味着如果你开发的站点没有JavaScript就不能
使用,或者你的JavaScript增强需要用户或用户代理在不低效运行得情况下具有一定的能力,那么你的客户就可以控告你歧视。
然而,JavaScript既不是邪恶的也不是没用的,它是一个优秀的工具,可以帮助在你平滑流畅的网站上冲浪的用户花费更少的时间。
正在阅读:
JavaSCRIPT 论文翻译05-31
预备党员十九大思想汇报范文02-24
新视野大学英语读写教程(第三版)第二册unit 1教案06-08
6.2立方根(公开课)07-27
美味素食菜1 - 图文06-03
土地增值税04-05
数电试卷和答案09-16
杭州市数字城管软件需求规格说明书01-11
生姜泡脚有哪些好处呢?10-13
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 论文翻译
- JavaSCRIPT
- 提高卡车空调系统性能的设计方法的研究
- 新准则新税法下工资与福利费的列支范围
- 11时空观的嬗变及其当代意义_韩宝华
- 2013会计继续教育考试及答案 医院会计制度
- 模板工程施工工艺标准
- 中考小说阅读解题技巧
- 2018年度四川省大数据时代的互联网信息安全试题及答案1
- 政府融资平台与地区经济发展问题探讨(黄煦凯)中华讲师网
- 400米标准半圆式田径场的画法
- 广东省房屋建筑和市政工程工程质量安全检测收费指导价
- 虚拟示波器开发软件的比较
- 的考研英语核心词汇
- CAD2007教程第5章_规划和管理图层
- 高考语文词语辨析专项检测100题(含详细解答)
- 新涵养汽车服务连锁加盟申请表
- 小学一年级10以内连加连减口算练习题1080道
- 第1章 网络互联设备和管理
- 信息工程监理实施细则
- 建筑工程概预算 第二章
- 第2章 多元回归分析