document.createElement("script")和window.onload执行顺序

时间: 2023-07-24 17:10:55 浏览: 70
`document.createElement("script")` 用于创建一个 `<script>` 元素,而 `window.onload` 是一个事件,在整个页面及其所引入的资源都加载完毕后触发。 执行顺序如下: 1. 首先,当解析到 `<script>` 标签时,会立即创建一个 `<script>` 元素,但不会立即执行它。 2. 接着,浏览器会继续解析和加载页面的其余内容。 3. 当整个页面及其所引入的资源都加载完毕后,`window.onload` 事件触发。 4. 在 `window.onload` 事件处理程序中,可以执行需要在页面加载完毕后执行的代码,包括动态创建的 `<script>` 元素的内容。 因此,`document.createElement("script")` 会在页面加载期间创建 `<script>` 元素,而 `window.onload` 事件处理程序中的代码会在页面加载完毕后执行。
相关问题

const print = () => { if (!baseInfo.orderCode) { message.error("打印正在准备,请5秒后再点击") return } const el = document.getElementById("print-container") const iframe = document.createElement("IFRAME") let doc = null // iframe.setAttribute('style', 'position:absolute;top:0px;left:0px;width:100%;height:100%',); document.body.appendChild(iframe) doc = iframe.contentWindow.document // 引入打印的专有CSS样式,根据实际修改 doc.write(`<LINK rel="stylesheet" type="text/css" href="./orderPrint.css">`) doc.write(el.innerHTML) doc.close() // 获取iframe的焦点,从iframe开始打印 iframe.contentWindow.focus() iframe.onload = () => { iframe.contentWindow.print() document.body.removeChild(iframe) } if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe) } const old = window.document.body.innerHTML const printPart = document.getElementById("print-container") window.document.body.innerHTML = "" window.document.body.appendChild(printPart) window.print() window.document.body.innerHTML = old window.location.reload() }优化这段代码

这段代码可以进行如下的优化: 1. 避免在函数内部操作 DOM,最好将其作为参数传递进来,这样可以提高代码的可重用性和可维护性。 ```javascript const print = (elId, cssUrl) => { const el = document.getElementById(elId) if (!el) { message.error("找不到打印内容,请检查elId") return } if (!baseInfo.orderCode) { message.error("打印正在准备,请5秒后再点击") return } const iframe = document.createElement("IFRAME") document.body.appendChild(iframe) const doc = iframe.contentWindow.document doc.write(`<LINK rel="stylesheet" type="text/css" href="${cssUrl}">`) doc.write(el.innerHTML) doc.close() iframe.contentWindow.focus() iframe.onload = () => { iframe.contentWindow.print() document.body.removeChild(iframe) } if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe) } } ``` 2. 将打印的专有CSS样式和打印的内容分开处理,以便更好地维护和修改样式。 ```javascript const print = (elId, cssUrl, printContent) => { const el = document.getElementById(elId) if (!el) { message.error("找不到打印内容,请检查elId") return } if (!baseInfo.orderCode) { message.error("打印正在准备,请5秒后再点击") return } const iframe = document.createElement("IFRAME") document.body.appendChild(iframe) const doc = iframe.contentWindow.document const link = document.createElement("link") link.rel = "stylesheet" link.type = "text/css" link.href = cssUrl doc.head.appendChild(link) doc.body.innerHTML = printContent || el.innerHTML iframe.contentWindow.focus() iframe.onload = () => { iframe.contentWindow.print() document.body.removeChild(iframe) } if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe) } } ``` 3. 将打印内容从 DOM 中分离出来,以便在不同的场合下方便地调用。 ```javascript const printContent = (elId) => { const el = document.getElementById(elId) if (!el) { message.error("找不到打印内容,请检查elId") return "" } return el.innerHTML } const print = (elId, cssUrl, printContent) => { if (!baseInfo.orderCode) { message.error("打印正在准备,请5秒后再点击") return } const iframe = document.createElement("IFRAME") document.body.appendChild(iframe) const doc = iframe.contentWindow.document const link = document.createElement("link") link.rel = "stylesheet" link.type = "text/css" link.href = cssUrl doc.head.appendChild(link) doc.body.innerHTML = printContent || printContent(elId) iframe.contentWindow.focus() iframe.onload = () => { iframe.contentWindow.print() document.body.removeChild(iframe) } if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe) } } ```

let myChart export default { mounted() { if (typeof window.echarts === 'function') { this.initEcharts() } else { // 动态引入较大类库避免影响页面展示 const script = document.createElement('script') // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算 script.src = 'static/echarts.js' script.onload = this.initEcharts.bind(this) document.head.appendChild(script) } }, methods: { initEcharts() { myChart = echarts.init(document.getElementById('echarts')) // 观测更新的数据在 view 层可以直接访问到 myChart.setOption(this.option) }, updateEcharts(newValue, oldValue, ownerInstance, instance) { // 监听 service 层数据变更 myChart.setOption(newValue) }, onClick(event, ownerInstance) { // 调用 service 层的方法 ownerInstance.callMethod('onViewClick', { test: 'test' }) } } } 这个程序发生了报错:Cannot read property '0' of undefined,请说明原因,和修改建议

这个错误可能是因为在初始化 chart 之前,没有正确地设置 option 属性。建议添加一个判断,在调用 setOption 之前先确保 option 已经被正确设置。修改后的代码如下: ``` let myChart export default { mounted() { if (typeof window.echarts === 'function') { this.initEcharts() } else { // 动态引入较大类库避免影响页面展示 const script = document.createElement('script') // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算 script.src = 'static/echarts.js' script.onload = this.initEcharts.bind(this) document.head.appendChild(script) } }, methods: { initEcharts() { myChart = echarts.init(document.getElementById('echarts')) // 确保 option 已经被正确设置 if (this.option) { myChart.setOption(this.option) } }, updateEcharts(newValue, oldValue, ownerInstance, instance) { // 监听 service 层数据变更 myChart.setOption(newValue) }, onClick(event, ownerInstance) { // 调用 service 层的方法 ownerInstance.callMethod('onViewClick', { test: 'test' }) } } } ``` 另外,建议在组件销毁时,调用 `myChart.dispose()` 方法来销毁 chart 实例,以避免内存泄漏。

相关推荐

function previewImage(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { var img = document.createElement("img"); img.src = e.target.result; img.alt = "照片"; img.onclick = function () { showPopup(img.src); }; // 隐藏原始的input元素 input.style.display = "none"; // 添加图片到input元素的父节点 input.parentNode.appendChild(img); }; reader.readAsDataURL(input.files[0]); } } function showPopup(imageSrc) { var popup = document.createElement("div"); var popupImg = document.createElement("img"); var scale = 1; // 设置弹出窗口中的图片 popupImg.src = imageSrc; popupImg.style.transform = scale(${scale}); // 设置悬浮窗样式 popup.style.position = "fixed"; popup.style.top = "10%"; popup.style.left = "10%"; popup.style.width = "80%"; popup.style.height = "80%"; popup.style.backgroundColor = "transparent"; popup.style.zIndex = "9999"; // 添加图片到悬浮窗 popup.appendChild(popupImg); document.body.appendChild(popup); // 居中弹出窗口 function centerPopup() { var windowHeight = window.innerHeight; var popupHeight = popup.offsetHeight; // 计算弹出窗口的垂直偏移量 var scrollTop = window.pageYOffset || document.documentElement.scrollTop; var offset = (windowHeight - popupHeight) / 2 + scrollTop; // 设置弹出窗口的垂直位置 popup.style.top = offset + "px"; } // 初始化居中弹出窗口 centerPopup(); // 添加滚动和调整窗口大小事件监听器 window.addEventListener("scroll", centerPopup); window.addEventListener("resize", centerPopup); // 添加点击事件监听器,点击其他区域时隐藏弹出窗口 popup.onclick = function () { popup.style.display = "none"; } // 移除滚动和调整窗口大小事件监听器 function removeListeners() { window.removeEventListener("scroll", centerPopup); window.removeEventListener("resize", centerPopup); } // 在窗口关闭时移除事件监听器 window.onbeforeunload = function () { removeListeners(); };我在添加图片后需要图片始终显示在当前页面中间位置,并且能够随着滚动条滑动,始终在当前页面中间显示

function showPopup(imageSrc) { var popup = document.createElement("div"); var popupImg = document.createElement("img"); var scale = 1; var isDragging = false; var startX, startY, translateX, translateY; // 设置弹出窗口中的图片 popupImg.src = imageSrc; popupImg.style.transform = scale(${scale}); // 加载完成后计算图片的宽高比例 popupImg.onload = function() { var imgWidth = popupImg.width; var imgHeight = popupImg.height; // 计算图片的缩放比例 var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 var widthScale = windowWidth / imgWidth; var heightScale = windowHeight / imgHeight; scale = Math.min(widthScale, heightScale); // 设置弹出窗口中的图片样式 popupImg.style.transform = scale(${scale}); popupImg.style.display = "block"; popupImg.style.margin = "auto"; function centerPopup() { var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 var popupRect = popup.getBoundingClientRect(); var popupWidth = popupRect.width; var popupHeight = popupRect.height; scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; var offsetLeft = (windowWidth - popupWidth * scale) / 2; var offsetTop = (windowHeight - popupHeight * scale) / 2 + scrollTop; // 设置弹出窗口的位置 popup.style.left = offsetLeft + "px"; popup.style.top = offsetTop + "px"; } // 设置悬浮窗样式 function setPopupStyle() { var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 popup.style.position = "fixed"; popup.style.width = windowWidth + "px"; popup.style.height = windowHeight + "px"; popup.style.backgroundColor = "transparent"; popup.style.zIndex = "9999"; popup.onclick = function (event) { if (event.target === popup) { popup.style.display = "none"; } }; } // 在设置图片加载完成后调用居中弹出窗口和设置悬浮窗样式的函数 popupImg.onload = function() { // 设置图片缩放比例 var imgWidth = popupImg.width; var imgHeight = popupImg.height; var widthScale = window.innerWidth * 0.8 / imgWidth; var heightScale = window.innerHeight * 0.8 / imgHeight; scale = Math.min(widthScale, heightScale); popupImg.style.transform = scale(${scale}); // 设置弹出窗口样式和居中位置 setPopupStyle(); centerPopup(); // 显示图片和悬浮窗 popupImg.style.display = "block"; popup.style.display = "block"; } // 添加图片到悬浮窗 popup.appendChild(popupImg); document.body.appendChild(popup);这段代码可以怎么优化

function showPopup(imageSrc) { var popup = document.createElement("div"); var popupImg = document.createElement("img"); var scale = 1; var isDragging = false; var startX, startY, translateX, translateY; // 设置弹出窗口中的图片 popupImg.src = imageSrc; popupImg.style.transform = scale(${scale}); // 加载完成后计算图片的宽高比例 popupImg.onload = function() { var imgWidth = popupImg.width; var imgHeight = popupImg.height; // 计算图片的缩放比例 var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 var widthScale = windowWidth / imgWidth; var heightScale = windowHeight / imgHeight; scale = Math.min(widthScale, heightScale); // 设置弹出窗口中的图片样式 popupImg.style.transform = scale(${scale}); popupImg.style.display = "block"; popupImg.style.margin = "auto"; // 居中弹出窗口 function centerPopup() { var windowHeight = window.innerHeight; var popupHeight = popup.offsetHeight; var scrollTop = window.pageYOffset || document.documentElement.scrollTop; var offset = (windowHeight - (imgHeight * scale)) / 2 + scrollTop; // 设置弹出窗口的垂直位置 popup.style.top = offset + "px"; } // 设置悬浮窗样式 popup.style.position = "fixed"; popup.style.top = "10%"; popup.style.left = "10%"; popup.style.width = windowWidth + "px"; popup.style.height = windowHeight + "px"; popup.style.backgroundColor = "transparent"; popup.style.zIndex = "9999"; // 添加图片到悬浮窗 popup.appendChild(popupImg); document.body.appendChild(popup); // 初始化居中弹出窗口 centerPopup(); // 添加滚动和调整窗口大小事件监听器 window.addEventListener("scroll", centerPopup); window.addEventListener("resize", centerPopup); // 点击事件监听器,点击其他区域时隐藏弹出窗口 popup.onclick = function () { popup.style.display = "none"; }; // 移除滚动和调整窗口大小事件监听器 function removeListeners() { window.removeEventListener("scroll", centerPopup); window.removeEventListener("resize", centerPopup); } // 在窗口关闭时移除事件监听器 window.onbeforeunload = function () { removeListeners(); }; };需要不管第几行第几列的图片放大时,放大的悬浮窗始终在当前页面最中心

// var agent, password, mode, phoneId //登录所需参数 // window.onload = function () { // init(); // }; //初始化电话条 function init() { var iframe = document.createElement('iframe'); iframe.name = "uinCall"; iframe.id = "uinCall"; iframe.ref = "uinCall"; iframe.allow = "microphone"; iframe.src = "https://ykf.uincall.com/telephonebarplugin/index.html"; // iframe.src = "https://ceshi.uincall.com/telephonebarplugin/index.html"; var telephoneBar = document.getElementById("telephoneBar"); if (telephoneBar) { telephoneBar.appendChild(iframe); var uinCall = document.getElementById("uinCall"); console.log('@@@-', uinCall); iframe.style.width = telephoneBar.getAttribute("data-width"); iframe.style.height = telephoneBar.getAttribute("data-height"); iframe.style.border = 0; } } //打印来自电话条内的抛出事件 window.addEventListener('message', function (e) { // console.log('@EchoData','message>>',e) console.info(e); if (e.data) { try { var resData = JSON.parse(e.data); // 动态修改iframe的高度 if (resData && resData.key && resData.key === 'changeCallHistoryState') { var telephoneBar = document.getElementById("telephoneBar"); var uinCall = document.getElementById("uinCall"); var frameHeight = telephoneBar.getAttribute("data-height"); if (resData.data) { // 显示 var heightConfig = parseInt(frameHeight); uinCall.style.height = (!heightConfig || heightConfig < 310) ? '310px' : heightConfig + 'px'; } else { uinCall.style.height = frameHeight; } } } catch (err) { console.info(e); } } }); //高级话务 function telephoneFunction(data) { let uincall = document.getElementById('uinCall'); if (uincall) { uincall.contentWindow.postMessage(data, '*'); } else { init(); uincall = document.getElementById('uinCall'); if (uincall) { setTimeout(() => { uincall.contentWindow.postMessage(data, '*'); }, 500); } } } 用vue优化代码编程

最新推荐

recommend-type

grpcio-1.47.0-cp310-cp310-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

小程序项目源码-美容预约小程序.zip

小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序v
recommend-type

MobaXterm 工具

MobaXterm 工具
recommend-type

grpcio-1.48.0-cp37-cp37m-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依