daydreamer
daydreamer

Reputation: 91959

Calling a function from another javascript file

I have javascript file in following directory

/static
       /handler.js
       /evercookie.js
       /jquery.js

All I want to do from handler.js function call a function in evercookie.js. for example
in handler.js

   var ec = new evercookie();
        cookiePresent = false
        ec.get(suggestion_id, function(value) { 
            alert("Cookie value is " + value);
            cookiePresent = true;
    });
    if (cookiePresent) {
            return;
        } else {
            ec.set(suggestion_id, "1");
            alert("cookie set for " + suggestion_id);
    }

where evercookie() is in evercookie.js

when I try this it fails saying evercookie not found

How shall I fix this?

Upvotes: 2

Views: 2230

Answers (2)

Alex
Alex

Reputation: 35399

Make sure that the order of your scripts is correct - evercookie.js needs to be referenced before any other script can use it.

From:

   /handler.js
   /evercookie.js
   /jquery.js

To:

   /jquery.js
   /evercookie.js
   /handler.js

The browser parses and interprets HTML, CSS and JavaScript top-down so if you try to access a property or instantiate an object that doesn't exist the browser will error...

More on Browser Parsing:

http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#overview-of-the-parsing-model

Upvotes: 3

Will
Will

Reputation: 1619

You have to either just put the evercookie.js code above the code in your handler.js file, or you have to link to evercookie.js BEFORE you link to handler.js.

<script src="static/evercookie.js">
<script src="static/handler.js">

Upvotes: 1

Related Questions