index.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta content="text/html;charset=utf-8" http-equiv="content-type">
- <title> </title>
- <script src=‘‘ id="s1"></script>
- <script src="dynamic.js"></script>
- </head>
-
- <body>
-
- </body>
- </html>
test.js
- alert("hello! I am test.js");
- var str="1";
dynamic.js
-
-
-
-
-
-
-
-
- function GetHttpRequest()
- {
- if ( window.XMLHttpRequest )
- return new XMLHttpRequest() ;
- else if ( window.ActiveXObject )
- return new ActiveXObject("MsXml2.XmlHttp") ;
- }
-
-
- function ajaxPage(sId, url){
- var oXmlHttp = GetHttpRequest() ;
- oXmlHttp.onreadystatechange = function()
- {
- if (oXmlHttp.readyState == 4)
- {
- includeJS( sId, url, oXmlHttp.responseText );
- }
- }
- oXmlHttp.open(‘GET‘, url, false);
- oXmlHttp.send(null);
- }
-
-
- function includeJS(sId, fileUrl, source)
- {
- if ( ( source != null ) && ( !document.getElementById( sId ) ) ){
- var oHead = document.getElementsByTagName(‘HEAD‘).item(0);
- var oScript = document.createElement( "script" );
- oScript.type = "text/javascript";
- oScript.id = sId;
- oScript.text = source;
- oHead.appendChild( oScript );
- }
- }
-
- ajaxPage( "scrA", "test.js" );
- alert( "主页面动态加载JS脚本。");
- alert( "主页面动态加载a.js并取其中的变量:" + str );
js动态引入的四种方式
原文:http://www.cnblogs.com/dingyufenglian/p/4817007.html