1. Dynamically loading scripts As the demand for websites grows, the demand for scripts also gradually increases. We have to introduce too many JS scripts and reduce the performance of the entire site, so the concept of dynamic scripts emerged to load the corresponding scripts at the right time. Copy code The code is as follows:<script type="text/javascript"> window.onload = function(){ alert(typeof BrowserDetect); } var flag = true; //Set true and then load if (flag) { loadScript('browserdetect.js'); //Set the loaded js } function loadScript(url) { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = url; //document.head.appendChild(script); //document.head means <head> document.getElementsByTagName('head')[0].appendChild(script); } </script>Dynamic execution of js</p> <p> <script type="text/javascript"> window.onload = function(){ } var flag = true; //Set true and then load if (flag) { var script = document.createElement('script'); script.type = 'text/javascript'; var text = document.createTextNode("alert('Lee')"); //IE6,7,8 browsers report an error script.appendChild(text); document.getElementsByTagName('head')[0].appendChild(script); } </script>IE 6, 7, and 8 browsers consider script to be a special element and cannot access child nodes. For compatibility, the text attribute can be used instead. </p> <p> <script type="text/javascript"> window.onload = function(){ } var flag = true; //Set true and then load if (flag) { var script = document.createElement('script'); script.type = 'text/javascript'; script.text = "alert('Lee')"; script.appendChild(text); document.getElementsByTagName('head')[0].appendChild(script); } </script> Need to be compatible with all browsers 2. Dynamically loading styles In order to dynamically load style sheets, such as switching website skins. There are two ways to load a style sheet, one is the <link> tag and the other is the <style> tag. Dynamic executionlink Copy code The code is as follows:var flag = true; if (flag) { loadStyles('basic.css'); } function loadStyles(url) { var link = document.createElement('link');link.rel = 'stylesheet'; link.type = 'text/css'; link.href = url; document.getElementsByTagName('head')[0].appendChild(link); } Dynamic execution style Copy code The code is as follows:<script type="text/javascript"> var flag = true; if (flag) { var style = document.createElement('style'); style.type = 'text/css'; //var box = document.createTextNode('#box{background:red}'); // IE6,7,8 not supported //style.appendChild(box); document.getElementsByTagName('head')[0].appendChild(style); insertRule(document.styleSheets[0], '#box', 'background:red', 0); } function insertRule(sheet, selectorText, cssText, position) { //If it is not IE6,7,8 if (sheet.insertRule) { sheet.insertRule(selectorText + "{" + cssText + "}", position); //If it is IE6,7,8 } else if (sheet.addRule) { sheet.addRule(selectorText, cssText, position); } } </script> |
<<: CSS to achieve Skeleton Screen effect
>>: Layui implements the login interface verification code
I recently deployed and tested VMware Horizon, an...
This article attempts to write a demo to simulate...
ab command principle Apache's ab command simu...
Table of contents 1. Use the uuid function to gen...
Table of contents 1. MySQL join buffer 2. JoinBuf...
After obtaining the system time using Java and st...
NodeJS copies the files: Generally, the copy oper...
Query Cache 1. Query Cache Operation Principle Be...
Idea: Just sort randomly first and then group. 1....
introduction Have you ever encountered a situatio...
Why learn vim Linux has a large number of configu...
Install grafana. The official website provides an...
But recently I found that using this method will c...
Preface In this article, we'll explore the ev...
MySQL 8.0.13 has a data folder by default. This f...