Kyle Schmidt
Kyle Schmidt

Reputation: 318

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 like, and would I have to do anything additional in the html page to make it work?

Upvotes: 0

Views: 188

Answers (3)

axel_c
axel_c

Reputation: 6796

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

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318488

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() {

    }
};

Upvotes: 1

CoolEsh
CoolEsh

Reputation: 3154

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.

Upvotes: 0

Related Questions