Crinsane
Crinsane

Reputation: 818

JavaScript object, call on page

I have one javascript file that houses all the functions for my website, stored it a large object, like this:

var Example = {

init : function(){
    alert('init');
},

page_one : function(){
    alert('this is page 1');
},

page_two : function(){
    alert('this is page 2');
}

}

Now I like to open a simple script tag on the different pages, and on page1 do Example.page_one(); and on page2 do Example.page_two();

But this doesn't work. When I call those in the same file as where die Example object is made, then it works, but not if I include that file in a page, and call it from there.

The Example object does show up in the window object

Can someone help me?

Upvotes: 0

Views: 106

Answers (2)

Hardik Soni
Hardik Soni

Reputation: 97

If you will create example object in your page where you have included it, it will override example object. Try to use direct functions rather than this approach.

Upvotes: 0

Crinsane
Crinsane

Reputation: 818

Well, I fixed it myself. Stupid fault.

My code was like this:

  <script defer src="js/scripts.js"></script>
  <script>
    AdminDashboard.notificationMessages();
    AdminDashboard.setupMenu();
    AdminDashboard.setupFileInput('#new_slideshow_image');
    AdminDashboard.setupSlideCrop();
  </script>

And the problem seemed to be with defer. The object did load, but after the script fired that called to it.

Thanks for the help!

Upvotes: 2

Related Questions