soleil
soleil

Reputation: 13073

jquery - call function in external js file

If I have this code in a file called custom.js:

var kennel = function(){this._init();};

kennel.prototype = {
    _init: function() {
        this.setListeners();
    },
    setListeners: function(){
        ...
    },
    getCats: function(){
        alert("Get cats");
    }
};

How do I call getCats() from some arbitrary html file?

Upvotes: 2

Views: 3766

Answers (2)

Shiki
Shiki

Reputation: 16808

This should be enough:

<script>
  var k = new kennel();
  k.getCats();
</script>

Here's a sample of it working: http://jsfiddle.net/NHaBb/

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

First of all you would include the javascript by using the <script> tag, generally in the <head>, then after that you can create another <script> tag which you can place your own javascript code in that is specific to the page, and which uses the functionality made available by the included file. For example:

<head>
    <script type="text/javascript" src="path/to/custom.js"></script>
    <script type="text/javascript">
        var myKennel = new kennel();
        myKennel.getCats(); // alerts "Get cats"
    </script>
</head>

Also, the code you have posted has nothing to do with jQuery - it is plain old, vanilla javascript.

Upvotes: 3

Related Questions