views:

35

answers:

2

I now understand that the following code will not work because I'm assigning window.onload to the result of the function, not the function itself. But if I remove the parens, I suspect that I have to explicitly call a separate function to process the config before the onload. So, where I now have:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<HEAD>
<script type="text/javascript" src="lb-core.js"></script>
<script type="application/javascript">
var lbp = {
    defaults: {
        color: "blue"
    },
    init: function(config) {
        if(config) {
            for(prop in config){
                setBgcolor.defaults[prop] = config[prop];
            }
        }
        var bod = document.body;
        bod.style.backgroundColor = setBgcolor.defaults.color;
    }
}
var config = {
    color: "green"
}
window.onload = lbp.init(config);
</script>
</HEAD>
<body>
<div id="container">test</div>
</body>
</HTML>

I imagine I would have to change it to:

var lbp = {
    defaults: {
        color: "blue"
    },
    configs: function(config){
            for(prop in config){
                setBgcolor.defaults[prop] = config[prop];
            }
    },
    init: function() {
        var bod = document.body;
        bod.style.backgroundColor = setBgcolor.defaults.color;
    }
}
var config = {
    color: "green"
}

lbp.configs(config);
window.onload = lbp.init;

Then, for people to use this script and pass in a configuration, they would need to call both of those bottom lines separately (configs and init). Is there a better way of doing this?

Note: If your answer is to bundle a function of window.onload, please also confirm that it is not hazardous to assign window.onload within scripts. It's my understanding that another script coming after my own could, in fact, overwrite what I'd assigned to onload. With that in consideration, it's best to expect your script user to call the function on initialization himself.

+2  A: 

Use an anonymous function to pass options, like this:

window.onload = function() { lbp.init(config); };

If you're worried about getting overwritten take the unobtrusive route, like this.

Alternatively you can use a library for your javascript needs, for example I use jQuery for this, and it would look like this:

$(function() { lbp.init(config); });

Getting a library for only this is overkill, but if you have lots of javascript going on, I'd suggest you take a look at what's available.

Nick Craver
Would it be bad to hardcode this into init and call it without parens? For example, I could test for lbpConfig, and if it exists, then I could apply its settings? This script won't be initialized multiple times on the same page...
Matrym
re: libraries - I love jQuery, but I'm trying to learn old school javascript. I'm also trying to build this without dependencies, so people can choose their own libraries when using it.
Matrym
@Matryn - It depends what you're after I guess, you can do `.init(config || {})` and it'll pass config if it'll exists, empty/no options if it doesn't.
Nick Craver
I think I'm making this more complicated than it needs to be. Is there any reason not to just have the user set their defaults by calling lbp.defaults {color:"red"}
Matrym
@Matrym - They can certainly call it that way, or `.init({ color: 'red'})`, just think about the order of things, will your code be in an external script? If that's the case make sure how and when they set the defaults executes before the `onload` event fires.
Nick Craver
A: 

You could return an anonymous function as a result of calling the init. Something like this:

var lbp = {
    defaults: {
        color: "blue"
    },
    init: function(config) {
        for(prop in config){
            setBgcolor.defaults[prop] = config[prop];
        }
        return function() {
            var bod = document.body;
            bod.style.backgroundColor = setBgcolor.defaults.color;
        };
    }
}
window.onload = lbp.init({color: "green"});
Martyn