开发者

Javascript equivilent of Python's include

开发者 https://www.devze.com 2023-02-14 18:48 出处:网络
I\'m hunting for the equivalent of Python\'s import statement. I\'d love for the following to work: <head>

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

精彩评论

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

关注公众号