2026/4/17 22:40:33
网站建设
项目流程
做网站的职员称呼什么,搜索引擎的营销方法有哪些,网站建设代理,网站开发 ssh 菜鸟HTML 版本不停的更新迭代#xff0c;也导致浏览器支持的写法眼花缭乱#xff0c;就拿 script 标签来说#xff0c;刚刚入行的那会儿就只知道用来写内联的 js 代码#xff0c;后来又学到了引入 js 文件#xff0c;ES 模块规范化之后又知道能用来引入模块化的 js 代码#…HTML 版本不停的更新迭代也导致浏览器支持的写法眼花缭乱就拿 script 标签来说刚刚入行的那会儿就只知道用来写内联的 js 代码后来又学到了引入 js 文件ES 模块规范化之后又知道能用来引入模块化的 js 代码可这就是它的全部了吗然而...并不是各种用法看看 script 千奇百怪的用法一定有你没见过的~~1、内联脚本如文章开头说的一样直接用来写内联脚本本公众号大部分文章都是使用内联脚本的方式所写scriptconst name 公众号前端路引console.log(name)/script2、引入外部脚本使用 src 属性直接引入外部脚本这是目前大部分前端项目的用法虽然 vite 直接使用 ES 模块化打包但要兼容低版本浏览器还是得转成普通的 js 文件引入script srcproject/path/script.js/script3、defer 延迟标签的 defer 属性可以控制脚本异步加载并且能让脚本顺序执行script defer srcproject/path/script1.js/scriptscript defer srcproject/path/script2.js/script以上脚本就算写在 head 标签中也不会阻止 dom 解析而且 script2.js 一定是在 script1.js 之后执行。4、async 异步async 也能控制脚本异步加载但不同的是 async 加载的脚本无法保证脚本执行顺序。script async srcproject/path/script1.js/scriptscript async srcproject/path/script2.js/script以上脚本没办法保证 script1.js 一定会先执行此属性一般多用于加载与项目流程无关的一些 js 文件比如说统计代码、广告代码等等。5、动态加载脚本使用 js 创建 script 标签引入 js 文件即可实现 js 脚本的动态加载const script document.createElement(script);script.src dynamic.js;script.onload function () {console.log(dynamic.js 加载成功);}script.onerror function () {console.log(dynamic.js 加载失败);}// 脚本插入到页面中才会开始加载document.head.appendChild(script);6、资源完整性校验integrity 属性能用于校验资源是否被篡改详细算法参考https://developer.mozilla.org/zh-CN/docs/Web/Security/Subresource_Integrityscript srchttps://cdn.xxx.com/jquery.js integritysha384-.../script7、跨域控制crossorigin 属性用于控制跨域请求的凭据传递script srchttps://other-domain.com/script.js crossoriginanonymous/scriptanonymous不发送凭据如 Cookiesuse-credentials发送凭据8、ES 模块使用 typemodule 即可在 script 标签内使用 ES 模块语法这与浏览器的版本有关一些低版本可能并不支持script typemodule srcmain.mjs/scriptscript typemoduleimport { foo } from ./foo.js;/script9、nomodule 兼容nomodule 属性用于兼容不支持 ES 模块的浏览器如果浏览器不支持 ES 模块则 nomodule 属性下的脚本会被执行反之则不会执行。script nomodule srcproject/path/fallback.js/script10、动态导入ES 模块允许在代码执行时导入模块此方式就称之为 动态导入script typemoduleconst isMobile navigator.userAgent.match(/mobile/i);if (isMobile) {import(./project/path/mobile.js).then(module {module.default();})}/script11、模块映射typeimportmap 属性允许指定 ES 模块的映射关系在后续书写时候就不必再写完整的模块路径可以只写模块名script typeimportmap{imports: {lodash: https://cdn.jsdelivr.net/npm/lodash4.17.21/esm}}/scriptscript typemoduleimport _ from lodash; // 实际加载 CDN 资源_.chunk([1, 2, 3], 2);/script12、模版字符串利用自定义的 type 属性可以将一些 HTML 字符串写在 script 标签中比如 vue 的模版字符串script typetext/x-template idindexdiv classindexh3点击历史记录跳转会保存滚动条位置/h3p v-for(item,index) in list :keyindextemplate v-ifindex % 8 ! 0{{ index }}/templatetemplate v-elsepa hrefjavascript:; click$router.go(1)历史记录前往下一页/a/prouter-link :to{ name: details }跳转前往详情页/router-link/template/p/div/scriptscriptvar list (new Array(50)).fill(1)Vue.component(Index, {template: #index,data: function () {return {list: list}},})/script实验性属性fetchpriority允许指定外部脚本的加载优先级。有效值high/low/autoblocking可以指定在脚本加载时浏览器中的页面渲染应该被阻断。有效值render