<script type=”text/javascript” src=”example.js”></script>
外链的javascript都有一个.js的后缀,但是这并不是必须的,浏览器不会去检查后缀
它的意义是让后台的脚本可以动态的生成js代码
但是注意虽然服务器会通过文件拓展名来决定用那种mime类型给请求
但如果你没有写.js的,确保你的服务器返回正确的mime类型
另外外链js可以来自不同的外部域,编译器不会进行区分
如果添加defer属性,就不会推迟编译,不中断浏览器加载Dom,意思就是告诉浏览器,这个外链脚本不会改变dom结构,可以等整个页面编译完成之后再执行(也就是编译完</HTML>),在load事件之前。但是不确定在domcontentloaded事件前后,在html5的定义中,它们会按顺序执行
Async属性也会让浏览器立刻下载该文件,但是他们不是按前后顺序一个个执行,并没有明确的执行顺序。这样的目的是为了告诉浏览器不需要等待这个脚本完全下载完执行完才继续加载页面。也是在load事件之前执行
额外好处:
- ➤ Maintainability — JavaScript code that is sprinkled throughout various HTML pages turns code maintenance into a problem. It is much easier to have a directory for all JavaScript files so that developers can edit JavaScript code independent of the markup in which it’s used.
- ➤ Caching — Browsers cache all externally linked JavaScript files according to specific settings, meaning that if two pages are using the same file, the file is downloaded only once. This ultimately means faster page-load times.
- ➤ Future-proof — By including JavaScript using external files, there’s no need to use the XHTML or comment hacks mentioned previously. The syntax to include external files is the same for both HTML and XHTML.