Reply LinkBack Thread Tools Search this Thread
Old 27-10-2006, 04:52   #1 (permalink)
freelancr
Senior Member
 
freelancr's Avatar
 
Join Date: Oct 2006
Posts: 2,076
Unobtrusive JS Question - window.onload external JS files?

I'm not too clued up on Javascript, so I tend to use 3rd party stuff like SWFObject. At the moment I just dump all these scripts into my "library.js" file, then use the <body onload="function1();function2();"> for the ones that need it.

This isnt a particularly good way of going about things, so was hoping to clean up the process a little. Was thinking if it is possible to have a .js file that I link from the head of my xhtml pages, which will then load up all the other .js files - so that way I only have to use the one line in the head to load up all my scripts. Also would be useful to keep them separate, so I can add or remove scripts and upgrade any .js files with newer versions as they get released.

And also, possibly unload them too when the page changes, or when they leave the site, or when they close the window - as the google maps api recommends it to prevent memory leaks.

Any help, or links most appreciated.
  Reply With Quote
Old 27-10-2006, 05:35   #2 (permalink)
freelancr
Senior Member
 
freelancr's Avatar
 
Join Date: Oct 2006
Posts: 2,076
http://adactio.com/atmedia2005/

Code:
function foo() { //do stuff } window.onload=foo;

Doing this at the moment, but means I have to have all my scripts in the one library.js file. If anyone can come back with any answers to the original question id be most greatful.
  Reply With Quote
Old 27-10-2006, 08:32   #3 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,130
what's wrong with having several js files in the head?
__________________
George Smith - a vain attempt to get higher on google for my name

  Reply With Quote
Old 07-11-2006, 23:41   #4 (permalink)
gregor
WHOA!
 
gregor's Avatar
 
Join Date: Jun 2005
Location: Toronto, Canada
Posts: 61
Quote:
Originally Posted by stickmus
what's wrong with having several js files in the head?

Yep... I agree. Have one file called global.js or base.js with functions you'll probably use a lot. My first recommendation would be adding addEvent to that file. See http://www.dustindiaz.com/top-ten-javascript/ for more info. Using addEvent is simple so you can forget about body onLoad crap:
Code:
function doThisFirst() { alert("Yay!"); } //when the window loads fire doThisFirst() addEvent(window, "load", doThisFirst); //or, call doThisFirst() when a link with id "theLink" is clicked addEvent(document.getElementById("theLink"), "click", doThisFirst); //or assign multiple onClick handlers like this: addEvent(document.getElementById("theLink"), "click", function() { doThisFirst(); thenDoThis(); thenThis(); });
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8