This is an article on how to add a javascript function that will be run when a webpage has loaded. We begin by defining a function for running after a page (or actually window) has been loaded:
function bodyOnLoad() {
..
..
}
And then we’ll do:
window.onload = bodyOnLoad;
However, we also want to make sure our setting of the load event doesn’t remove some other setting. This is done by also keeping any older events.
We store the previous on load even by doing;
var prevOnLoad = window.onload;
And we redefine our bodyOnLoad function:
function bodyOnLoad() {
prevOnLoad();
..
..
}
However we can make the creation of the function and the setting of the event a little bit more effective by doing:
window.onload = function() {
prevOnLoad();
..
..
}
You still need to get prevonload before you do that
This becomes even more obvious once we create a function for adding new load events:
function addLoadEvent(func) {
var prevOnLoad = window.onload;
window.onload = function() {
prevOnLoad();
func();
}
}
In this way we can concentrate on creating the new load event outside of the function for adding it to the window.onload.
function myEvent(){
..
..
}
addLoadEvent(myEvent);
We might even do:
addLoadEvent(
function (){
..
..
}
);
Notice the difference between curly braces “{}” and parenthesis “()”
Finally, we have to make sure there is a load event set for the window before calling it from the new event, so we need to check for this:
function addLoadEvent(func) {
var prevOnLoad = window.onload;
if (typeof prevOnLoad != 'function') {
window.onload = func;
}
else {
window.onload = function() {
prevOnLoad();
func();
}
}
}
In case you wondered. Sure, programming can be humorous, but this is more about looking at programming with humor. Or, well I’ve found a few funny things I’d like to share… XML is like violence: if it doesn’t solve your problem, you’re not using enough of it.
People who make buttumptions about their search and replace options, will be embarbutted when they repeat this clbuttic mistake.
When looking at other programmer’s code I’m sometimes surprised with things like this:
exportDocumentView(java.lang.String absPath, java.io.OutputStream out,
boolean skipBinary, boolean noRecurse)
And in such prominent frameworks as Java Content Repository as well.
What’s my problem then? Double negations. In order to do exportDocumentView and get binary data and recursive export you’ll need to do:
exportDocumentView(“/path/to/my/Node/”, System.out, false, false)
Sure I can handle it… but… false to opt something in? I find it rather fishy. Call me an idiot but my brain just don’t deal with that kind of stuff easily…
What I would have wanted instead was:
exportDocumentView(java.lang.String absPath, java.io.OutputStream out,
boolean includeBinary, boolean recursive)
Which would have been called like:
exportDocumentView(“/path/to/my/Node/”, System.out, true, true)
For when we want binary data and recursive export, and like this for the case when we don’t want either or both:
exportDocumentView(“/path/to/my/Node/”, System.out, false, true)
exportDocumentView(“/path/to/my/Node/”, System.out, true, false)
exportDocumentView(“/path/to/my/Node/”, System.out, false, false)
I’m just saying. In my world false means “no” and “no” means don’t give me something or don’t do something:
“Don’t return binary data.”
“Don’t recurse the tree of nodes.”
To be compared with:
“Don’t skip binary data.”
“Don’t do no recursing.”
(But you’re free to curse?
)