1

I'm new to jQuery and javascript. I'm trying out a few things here so let me explain what i have:

-I have several seperate .js files running code. I cannot get them to load BEFORE the code in index.html file fires. It always loads the jQuery file (which sets the window property) and then loads index.html, and THEN all the other .js files i have list with the jQuery main file. So I'm trying to work around this.

I have seperated my javascript into seperate files:
-jQuery.js (mini) //from any local or main ajax source this works
-mainpage.js //javascript code for the main page setup. its there for as needed elements, currently-nothing.
-mainmenu.js //this loads mainmenu.html which conatains the mainmenu list.
-index.html //The only code i want to run on this is to declare ".active" class to the home button. I want it in this file so i can use the same universal mainmenu.js and mainmenu.html in all my files.
These are all declared in index.html's head.

My problem is it loads jQuery.js loads and then it fires index.html's javascript immediately. I connot figure out how to declare the function that will set #home button as ".active" Can i declare it to the document level? i know i shouldnt make it global... but i dnt even know how to do that?
i'm currently do this:

defineactive(){
    $(mmlist).find("#home").addClass("active"); 

}

however, mmlist hasn't been defined yet. That gets declared in mainmenu.js, which is loaded AFTER index.html

So i guess my question is Can I declare a function to make able to be called on outside the jQuery.js?

If not, besides using an outside source/app how can i make sure the javascript files are loaded in order? I plan on using this idea in more complex ways later so I need this to work.

Jexadox
  • 23
  • 6
  • 1
    First question, are your js files `jquery` dependent? – Hackerman Oct 17 '17 at 18:10
  • yes they are. thus i need jQuery to load FIRST, and that it does. but i then want my mainpage.js, mainmenu.js, and others to load NEXT, and the javascript at the end of my index.html (or other web pages) to fire LAST. – Jexadox Oct 17 '17 at 23:03

1 Answers1

0

Try to wrap your code as:

$(document).ready(function(){
// ... Your code ...
});

so it will run by the time when page will be loaded.

Flying
  • 4,104
  • 2
  • 14
  • 24
  • I always do this. did not work. according to another post this waits for the html to load but not all resources. trying `$(document).load(function(){` was supposed to fix that... but i tried that too to no avail. – Jexadox Oct 17 '17 at 23:02