javascript编写_如何在HTML页面上编写JavaScript

javascript编写

JavaScript is executed client-side, on the browser. Like , it can be written either inline, embedded, or linked, and (just like CSS) those methods are in order of preference in terms of power and adaptability. A link to a JavaScript file is often written inside the <head> section of the page on which it is used:

JavaScript在浏览器的客户端执行。 像一样,它可以内联,嵌入式或链接形式编写,并且(就像CSS一样)这些方法在功能和适应性方面按优先顺序排列。 指向JavaScript文件的链接通常写在使用该文件的页面的<head>部分中:

<head>
	<title>An HTML Page With Linked Javascript<title>
	<script src="assets/scripts/prettify.js"></script>
</head>

There are a few possible downsides to this:

有一些可能的缺点:

  1. Page rendering may be halted until the script is loaded and executed.

    在脚本加载和执行之前,页面渲染可能会停止。
  2. A script written in the <head> may attempt to modify DOM elements that have not yet appeared on the page.

    <head>编写的脚本可能会尝试修改尚未出现在页面上的DOM元素。

Alternatively, links to JavaScript files can be placed at the end of the document, typically just before the closing </body> tag.

另外,可以将JavaScript文件的链接放在文档的末尾 ,通常在</body>标记之前。

<script src="assets/scripts/prettify.js"></script>
</body>
</html>

…thus avoiding both issues.

…因此避免两个问题。

JavaScript files must have a .js extension in the filename. (You will occasionally see a language attribute used when linking to a JavaScript file, but that is deprecated and invalid in HTML.)

JavaScript文件的文件名必须具有.js扩展名。 (您有时会看到链接到JavaScript文件时使用的language属性,但是该属性已弃用,并且在HTML中无效。)

It is also possible to link to a JavaScript file outside your own domain: this is frequently done when using JavaScript frameworks such as jQuery to centralize code and potentially speed up load times. (Cross-domain files will load in a seperate simultaneous “stream” from the rest of the page, and will be cached by the browser: if the user has previously visited a site that uses the same technique for the same file, the cached version of the file will be used, rather than downloading it again.)

也可以链接到您自己域外JavaScript文件:使用jQuery等JavaScript 框架集中代码并可能加快加载时间时,通常会这样做。 (跨域文件将从页面的其余部分加载到单独的同时“流”中,并由浏览器缓存:如果用户以前访问过使用相同技术处理同一文件的站点,则缓存版本的文件将被使用,而不是再次下载。)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">
</script>

The second and less preferable option is to write the JavaScript embedded, again in the <head> section, or, preferably, at the bottom of the page:

第二种也是不太可取的选择是再次在<head>部分,或者最好在页面底部,编写嵌入JavaScript:

<script>
/* JavaScript code goes here */
</script>

Note that you cannot write HTML or CSS directly inside JavaScript; like CSS, using another language between the opening and closing <script> tags, or in the linked JavaScript file, will cause it to stop working.

请注意,您不能直接在JavaScript中编写HTMLCSS 。 例如CSS,在开始和结束<script>标记之间或链接JavaScript文件中使用另一种语言,将导致它停止工作。

You do not, as a general rule, write JavaScript code inline, for reasons I will cover shortly.

通常,您不会内联编写JavaScript代码,因为稍后我会介绍原因。

翻译自: https://thenewcode.com/191/How-To-Write-JavaScript-On-An-HTML-Page

javascript编写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值