동적 로드 js 파일

1445 단어 JavaScripthtml
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
<script type="text/javascript">
	function importScript(scriptPath, callback) {
		var headTag = document.getElementsByTagName("head")[0];
		
		var scriptTag = document.createElement("script");
		scriptTag.type = "text/javascript";
		var isOver = false;
		scriptTag.onload = function() {
			if (isOver) {
				return;
			}
			isOver = true;
			callback();

		}
                // IE9/10    onreadystatechange & onload
		scriptTag.onreadystatechange = function() {
			
			if (isOver) {
				return;
			}
			
			isOver = true;
			if (this.readyState == 'loaded' || this.readyState == 'complete') {
				callback();
			}
		};
		scriptTag.src = scriptPath;
		headTag.appendChild(scriptTag);
	}
	window.onload = function() {
		importScript("a.js", function() {test();});
	};
</script>
</head>
<body>

</body>
</html>

좋은 웹페이지 즐겨찾기