The examples in this article summarize the methods of dynamically loading js. Share it with everyone for your reference. The details are as follows:
Method 1: Direct document.write (asynchronous)
Since this method is asynchronous loading, document.write will rewrite the interface, which is obviously not practical
Method 2: Dynamically change the src attribute of an existing script (asynchronous)
This method will not change the interface elements or rewrite the interface elements, but it will also be loaded asynchronously
Method 3: Dynamically create script elements (asynchronous)
The advantage of this method compared to the second method is that there is no need to write a script tag in the interface at the beginning. The disadvantage is asynchronous loading
Method 4: XMLHttpRequest/ActiveXObject loading (asynchronous)
* Load js script asynchronously
* @param id The id of the <script> tag that needs to be set <br /> * @param url Relative path or absolute path of js file <br />*/ <br /> loadJs:function(id,url){ <br /> <br /> var xmlHttp = null; <br /> if(window.ActiveXObject){//IE <br /> try { <br /> //Can be used in IE6 and later versions <br /> xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); <br /> } catch (e) { <br /> //IE5.5 and later versions can be used <br /> xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); <br /> } <br /> }else if(window.XMLHttpRequest){ <br /> //Firefox, Opera 8.0, Safari, Chrome <br /> xmlHttp = new XMLHttpRequest(); <br /> } <br /> //Adopt synchronous loading <br /> xmlHttp.open("GET",url,false); <br /> //Send synchronous request, <br /> //If the browser is Chrome or Opera, it must be released before it can run, otherwise an error will be reported <br /> xmlHttp.send(null); <br /> xmlHttp.onreadystatechange = function(){ <br /> //4 means the data has been sent <br /> if(xmlHttp.readyState == 4){ <br /> //0 is the local area being accessed, 200 to 300 represents successful access to the server, <br /> //304 means the cache is accessed without modification <br /> If((xmlHttp.status >= 200 && xmlHttp.status <300) || xmlHttp.status == 0 || xmlHttp.status == 304){ <br /> var myBody = document.getElementsByTagName("BODY")[0]; <br /> var myScript = document.createElement( "script" ); <br /> myScript.language = "javascript"; <br /> myScript.type = "text/javascript"; <br /> myScript.id = id; <br /> try{ <br /> //IE8 and below do not support this method and need to be set through the text attribute <br /> myScript.appendChild(document.createTextNode(xmlHttp.responseText)); <br /> }catch (ex){ <br /> myScript.text = xmlHttp.responseText; <br /> } <br /> myBody.appendChild(myScript); <br /> } <br /> } <br /> } <br /> //Use asynchronous loading <br /> xmlHttp.open("GET",url,true); <br /> xmlHttp.send(null); <br /> }</script>
Setting it to false in open means synchronous loading. Synchronous loading does not require setting the onreadystatechange event
These four methods are all executed asynchronously, that is, while loading these scripts, the scripts on the main page continue to run.
Method 5: XMLHttpRequest/ActiveXObject loading (synchronization)
* js 스크립트를 동기적으로 로드
* @param id 설정해야 하는 <script> 태그의 ID <br /> * @param url js 파일의 상대 경로 또는 절대 경로 <br /> * @return {Boolean} 로딩 성공 여부를 반환합니다. true는 성공, false는 실패를 나타냅니다. <br />*/ <br /> loadJs:function(id,url){ <br /> <br /> var xmlHttp = null <br /> if(window.ActiveXObject){//IE <br /> {<br />을 시도해 보세요. //IE6 이상 버전에서 사용 가능 <br /> xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") <br /> } 잡기 (e) { <br /> //IE5.5 이상 버전 사용 가능<br /> xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") <br /> } <br /> }else if(window.XMLHttpRequest){ <br /> //파이어폭스, 오페라 8.0, 사파리, 크롬 <br /> xmlHttp = 새로운 XMLHttpRequest() <br /> } <br /> //동기 로딩 사용 <br /> xmlHttp.open("GET",url,false) <br /> //동기 요청을 보냅니다. 브라우저가 Chrome 또는 Opera인 경우 실행하기 전에 게시해야 합니다. 그렇지 않으면 오류가 보고됩니다. <br /> xmlHttp.send(널) <br /> //4는 데이터가 전송되었음을 의미합니다. <br /> if(xmlHttp.readyState == 4){ <br /> //0은 로컬 액세스, 200~300은 서버 액세스 성공, 304는 수정 없이 캐시 액세스 <br /> If((xmlHttp.status >= 200 && xmlHttp.status <300) || xmlHttp.status == 0 || xmlHttp.status == 304){ <br /> var myBody = document.getElementsByTagName("BODY")[0] <br /> var myScript = document.createElement( "script" ); myScript.언어 = "자바스크립트" <br /> myScript.type = "텍스트/자바스크립트" <br /> myScript.id = 아이디 <br /> 시도해 보세요{ <br /> //IE8 이하는 이 방식을 지원하지 않으므로 text 속성으로 설정해야 합니다. <br /> myScript.appendChild(document.createTextNode(xmlHttp.responseText)) <br /> }잡다(예){ <br /> myScript.text = xmlHttp.responseText <br /> } <br /> myBody.appendChild(myScript) <br /> true를 반환합니다. <br /> }그밖에{ <br /> 거짓 반환 <br /> } <br /> }그밖에{ <br /> 거짓 반환 <br /> } <br /> }<br /></script>

js字符串转数组的方法:1、使用“split()”方法,可以根据指定的分隔符将字符串分割成数组元素;2、使用“Array.from()”方法,可以将可迭代对象或类数组对象转换成真正的数组;3、使用for循环遍历,将每个字符依次添加到数组中;4、使用“Array.split()”方法,通过调用“Array.prototype.forEach()”将一个字符串拆分成数组的快捷方式。

Vue中处理组件的动态加载和切换Vue是一个流行的JavaScript框架,它提供了各种灵活的功能来处理组件的动态加载和切换。在本文中,我们将讨论一些Vue中处理组件动态加载和切换的方法,并提供具体的代码示例。动态加载组件是指根据需要在运行时动态加载组件。这样可以提高应用程序的性能和加载速度,因为只有当需要时才会加载相关的组件。Vue提供了async和awa

这篇文章主要为大家详细介绍了js实现打字小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

Python实现无头浏览器采集应用的页面动态加载与异步请求处理功能解析在网络爬虫中,有时候需要采集使用了动态加载或者异步请求的页面内容。传统的爬虫工具对于这类页面的处理存在一定的局限性,无法准确获取到页面上通过JavaScript生成的内容。而使用无头浏览器则可以解决这个问题。本文将介绍如何使用Python实现无头浏览器来采集使用动态加载与异步请求的页面内容

php在特定情况下可以读js内部的数组。其方法是:1、在JavaScript中,创建一个包含需要传递给PHP的数组的变量;2、使用Ajax技术将该数组发送给PHP脚本。可以使用原生的JavaScript代码或者使用基于Ajax的JavaScript库如jQuery等;3、在PHP脚本中,接收传递过来的数组数据,并进行相应的处理即可。

js全称JavaScript,是一种具有函数优先的轻量级,直译式、解释型或即时编译型的高级编程语言,是一种属于网络的高级脚本语言;JavaScript基于原型编程、多范式的动态脚本语言,并且支持面向对象、命令式和声明式,如函数式编程。

js原生选择器有getElementById()、getElementsByClassName()、getElementsByTagName()、querySelector()和querySelectorAll()等。详细介绍:1、getElementById()通过元素的唯一标识符来选择元素,它返回具有指定ID的元素作为结果等等。

Golang热更新原理探究:动态加载与重载的奥秘引言:在软件开发领域,程序员们经常希望能够在不重启应用的情况下进行代码修改和更新。这样的需求对于开发效率和系统运行的可靠性都具有重要意义。而Golang作为一门现代化的编程语言,为开发者提供了许多便捷的机制来实现热更新。本文将深入探讨Golang热更新的原理,特别是动态加载和重载的奥秘,并将结合具体的代码示例进


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
