Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-26-2010, 09:05 PM   PM User | #1
Grammaticus
New to the CF scene

 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Grammaticus is an unknown quantity at this point
Loading Multiple JavaScripts on the same page

Basically, I have a total of three different JavaScripts that need to be loaded when the user opens the page; let's call them script_a.js, script_b.js, and script_c.js

script_a.js and script_b.js each need to be loaded on every page; however, they do very different things (one displays text, the other creates buttons), and in the interest of readability and ease of editing later, I want to keep them separate.

script_c.js only needs to be loaded on one page; there will be a few variants (script_c2, script_c3, etc), but again, each of them only needs to be loaded on a single page.

The scripts are also too large and unwieldy for me to put them directly into the HTML (basically because I have to do a lot of different switches, and conditionals, and output formatting).

At present, the only way I know how to load a JavaScript is to use window.onload, so obviously I'm getting into the issue of competing onLoads.

Any help would be appreciated!
Grammaticus is offline   Reply With Quote
Old 07-26-2010, 09:24 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This topic has been covered many times in this forum.

http://www.javascriptkit.com/javatut...iplejava.shtml
http://www.dyn-web.com/tutorials/combine.php
Or you can simply fire a set of functions when the page loads.
Code:
<script type="text/javascript">
window.onload = function() {
functionOne();
functionTwo();
}
</script>
"When I was a kid I used to pray every night for a new bike. Then I realised that The Lord doesn't work that way, so I stole one and asked him to forgive me."
Philip M is offline   Reply With Quote
Old 07-26-2010, 09:46 PM   PM User | #3
Grammaticus
New to the CF scene

 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Grammaticus is an unknown quantity at this point
I'm sorry, I need a little bit more guidance on how to use this...

Code:
<script type="text/javascript">
I get this; this is what I'm currently using for the single javascript I have implemented and working.

Code:
window.onload =  function(){
So this here is saying "When the window loads, do the following:", yes?

Code:
functionOne();
functionTwo();
Here's what I don't understand - how do I call up to three different JavaScripts here?

Code:
script_a.js;
script_b.js;
script_c.js;
Is that how that would work? And within the code, how do I make sure that the right function loads when the script is called? There are multiple functions feeding into the main output function in each script - how do I get that one function to load when the script is called?
Grammaticus is offline   Reply With Quote
Old 07-26-2010, 10:28 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,202
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You don't call the actual *files*. You do, indeed, need to know the name of the function to be invoked *from* each file.

If each of those files is currently doing its own window.onload=xxxx, then you simply can find those lines and combine the xxxx's as Philip showed you.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 07-26-2010, 10:47 PM   PM User | #5
Grammaticus
New to the CF scene

 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Grammaticus is an unknown quantity at this point
Okay, I've got that code in the header now, but I'm unclear on how to specify two different external sources for each function.
Grammaticus is offline   Reply With Quote
Old 07-26-2010, 11:10 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,202
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Grammaticus View Post
Okay, I've got that code in the header now, but I'm unclear on how to specify two different external sources for each function.
Sorry. You've lost me. Are you asking how to tell that "universal" onload:
Code:
<script type="text/javascript">
window.onload = function() {
    functionOne();
    functionTwo();
}
</script>
where functionOne and functionTwo are?? You don't need to. The names must be unique, so JS will find them, whichever file they are in.

If your names aren't unique, then you've got problems. Not insurmountable ones, but problems.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 07-27-2010, 12:58 AM   PM User | #7
Grammaticus
New to the CF scene

 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Grammaticus is an unknown quantity at this point
Okay, now I'm lost... well, even more lost than I was before . Let me see if I can try to explain myself a little better.

In this block of code:
Code:
<script type="text/javascript">
window.onload = function() {
    functionOne();
    functionTwo();
}
</script>
FunctionOne resides in a file called "script_a.js".

FunctionTwo resides in a file called "script_b.js".

I don't understand how I use this to say "look for FunctionOne in script_a.js".

Assumption: Based on the "<script type=...>" syntax, I'm assuming that this code goes in the HTML header. If that's incorrect, I would appreciate the clarification.
Grammaticus is offline   Reply With Quote
Old 07-27-2010, 01:52 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,202
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Maybe I'm lost, too.

I have *assumed* that you have gotten this far:
Code:
<html>
<head>
<script type="text/javascript" src="script_a.js"></script>
<script type="text/javascript" src="script_b.js"></script>
<script type="text/javascript">
window.onload = function() {
    functionOne();
    functionTwo();
}
</script>
</head>
<body>
...
</body>
</html>
And that indeed the two functions are in the two ".js" files, as you described.

And you say that still does nothing?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 07-27-2010, 02:53 AM   PM User | #9
Grammaticus
New to the CF scene

 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Grammaticus is an unknown quantity at this point
OH! Yes, this works beautifully!

I figured out what was wrong, I accidentally used one of the JavaScript reserved words in one of my function names. But now this is working PERFECTLY, thank you so much!
Grammaticus is offline   Reply With Quote
Reply

Bookmarks

Tags
external, javascript, multiple, onload

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:22 PM.


Advertisement
Log in to turn off these ads.