开发者

Javascript equivilent of Python's include

I'm hunting for the equivalent of Python's import statement.

I'd love for the following to work:

<head>      
    <script type="text/javascript" src="foo.js"></script>
    <script type="text/javascript">
        function useBar(){
            alert(foo.bar());
        }
    </script>
</head>
<body>
    <button type="button" onClick="useBar();">Bar</button>

What would foo.js look lik开发者_如何学Ce, and would I have to do anything additional in the html page to make it work?


You mean python's import, right?

If you want namespaces, you can do something like that in foo.js.

var foo = {
    bar: function() {

    },
    baz: function() {

    }
};


foo.js contains a portion of js code (functions, objects, plain code). Whin this file was loaded throught <script type="text/javascript" source="foo.js"></script> all of its code now available across page.


foo.js would be just a regular javascript file, with functions and/or variables, with no <script> tags, nothing else required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜