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
Latest solution: -v /usr/share/zoneinfo/Asia/Shan...
<br />Looking at this title, you may find it...
<br />Previous article: Web Design Tutorial ...
Table of contents 1. Custom instructions 1. Regis...
1.fullpage.js Download address https://github.com...
Table of contents Various ways to merge objects (...
Purpose: 1. In order to map the server's exte...
Introduction to Selenium Grid Although some new f...
The scroll-view of WeChat applet has more bugs wh...
Two parameters of Mysql paging select * from user...
Intro Previously, our docker images were stored i...
VMware Preparation CentOS preparation, here is Ce...
The same server simulates the master-slave synchr...
Introduction Dockerfile build run is a manual ope...
After installing VMware and creating a new virtua...