Unobtrusive Javascript - body onload
Posted: Thu Oct 19, 2006 1:27 pm
Hello... I've got a couple javascript functions that I would like to be executed on page load. I do not, however want to use <body onload="blabla">. Here's what I have right now, but it only allows one function to be executed. Isn't there a way to attach more events to body onload? Thanks guys. I'd consult my javascript book, but I forgot it today:
Code: Select all
window.onload = function(){
Map = new UCA_Map('map', [39.69873414348139, -122.0745849609375]);
var map = Map.map;
// ====== Restricting the range of Zoom Levels =====
// Get the list of map types
var mt = map.getMapTypes();
// Overwrite the getMinimumResolution() and getMaximumResolution() methods
for (var i=0; i<mt.length; i++) {
mt[i].getMinimumResolution = function() {return 8;}
}
// Add a move listener to restrict the bounds range
GEvent.addListener(map, "move", function() {
checkBounds();
});
// The allowed region which the whole map must be within
var allowedBounds = new GLatLngBounds(new GLatLng(37,-123), new GLatLng(41,-119.5));
// If the map position is out of range, move it back
function checkBounds() {
// Perform the check and return if OK
if (allowedBounds.contains(map.getCenter())) {
return;
}
// It`s not OK, so find the nearest allowed point and move there
var C = map.getCenter();
var X = C.lng();
var Y = C.lat();
var AmaxX = allowedBounds.getNorthEast().lng();
var AmaxY = allowedBounds.getNorthEast().lat();
var AminX = allowedBounds.getSouthWest().lng();
var AminY = allowedBounds.getSouthWest().lat();
if (X < AminX) {X = AminX;}
if (X > AmaxX) {X = AmaxX;}
if (Y < AminY) {Y = AminY;}
if (Y > AmaxY) {Y = AmaxY;}
//alert ("Restricting "+Y+" "+X);
map.setCenter(new GLatLng(Y,X));
}
}
window.onunload = function(){
GUnload();
}