한국어

Develop

Javascript js 파일 동적 로딩

2014.11.06 10:27

kaiserhan 조회 수:50364

js 파일 동적 로딩


<!DOCTYPE HTML>

<html>

<head>

<title></title>

<script type="text/javascript">

<!--

function loadScript(url, callback) {

var scriptEl = document.createElement('script');

scriptEl.type = 'text/javascript';

// IE에서는 onreadystatechange를 사용

scriptEl.onload = function () {

callback();

};

scriptEl.src = url;

document.getElementsByTagName('head')[0].appendChild(scriptEl);

}

loadScript('example.js', function () {

// example.js가 로딩 완료한 시점에 실행

alert(nA);

});

defineModule('util', {

trim: function () { 

//

},

extend: function () {

//

}

});

//-->

</script>

</head>

<body>

</body>

</html>



var nA=2;

var foo = (function () {

    var i = 0;

    function init() {

        reset();

    }

    function reset() {

        i = 0;

    }

    function increase() {

        i++;

    }

    function decrease() {

        i--;

    }

    function get() {

      return i;

    }

    return {

        init: init,

        increase: increase,

        decrease: decrease,

        get: get

    };

}());

var Foo = (function () {

    var NAME = 'Foo';

    // 생성자 함수

    function Foo() {

        this.i = 0;

    }

    Foo.prototype.getClassName = function () {

        return NAME;

    };

    Foo.prototype.increase = function () {

        this.i++;

    };

    Foo.prototype.decrease = function () {

        this.i--;

    };

    return Foo; 

}());