# Solved: Multiple JavaScripts = Conflict



## voltticus (Aug 24, 2005)

I'm trying to get multiple external javascript files that I have made to work together. Only one of them loads.

The only fix I have seen was to put the functions in the tag (i.e 

Is there a better more dynamic way of doing this?

Thanks.


----------



## ehymel (Aug 12, 2007)

No reason they wouldn't all load. Maybe you could post some code so we can better help? The little snippet of code you posted has nothing to do with loading an external js file.


----------



## voltticus (Aug 24, 2005)

I solved the problem. 
At the end of every one of my external .js files I made a *window.onload = function* that would call all of the functions within the file once the web browser was loaded -

*window.onload = function*(){
allFunctionInFileName();
};

Problem here is say I have 3 external .js files to load that contained the window.onload function, it would only load the 3rd file because the window.onload function I created would overwrite one another and prevent the other two from working.

My fix = *window.addEventListener("load", functionName, false)* instead of _window.onload = function(){}_ at the end of each of my files. This way they don't interfere with one another and they work together.

An alternative fix I also found was to combine all of my JavaScript into one file and I could have technically ran the window.onload = function(){}.

However, from what I'm doing...addEventListener works.


----------



## tomdkat (May 6, 2006)

voltticus said:


> My fix = *window.addEventListener("load", functionName, false)* instead of _window.onload = function(){}_ at the end of each of my files. This way they don't interfere with one another and they work together.


I like this solution. :up:

Peace...


----------



## ehymel (Aug 12, 2007)

I think there's some terminology differences going on... your solution simply runs multiple functions upon page loading. Your original question was about loading multiple external js include files. 

Obviously you've found a solution for what you are wanting, so the point is moot.


----------



## voltticus (Aug 24, 2005)

ehymel said:


> I think there's some terminology differences going on... your solution simply runs multiple functions upon page loading. Your original question was about loading multiple external js include files.
> 
> Obviously you've found a solution for what you are wanting, so the point is moot.


Well what was happening is I had multiple external JavaScript files in the head of my HTML doc -





And each script had a window.onload function at the bottom of the page _(for loading all of the functions of that file when browser loaded)_ -

window.onload = function(){ 
functionNameInFile();
};

But, when the browser had loaded. ONLY "script-4.js" would run. That is because the _window.onload = function_ I created for each file, would overwrite each other. Then once the browser loaded, it would only pick up the last script "script-4.js".


----------



## ehymel (Aug 12, 2007)

Again, probably just a terminology issue, but...

There is no need to "load" a function. Functions are run when they are needed. Sometimes functions are needed to run when a page loads, and that is what you appear to be doing. Hard to say if this is right from what you posted, and since you have things fixed it doesn't really matter, but should anyone come across this thread in the future I don't want them to be confused.


----------



## voltticus (Aug 24, 2005)

Yeah that make sense. I am a JS beginner so I know what makes sense in my mind is going to come out completely different from what a person who actually knows JS. 

Yes, I was simply wanting to run my functions as the page loaded. 

I'm sure when I get the fully understanding of how the language works, I'll come back to this post and laugh.


----------

