HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Var scope on the same line

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
varsamethelinescope

Problem

Is there a better way I can code this? It's old code and I'm not sure makes sense today. JSLint gives me a lot of errors too. I don't have experience with JavaScript and I would like to optimize this snippet.

Any suggestion? Maybe I can achieve better code with JQuery?


// 


This is how an handler is called for example:


// 
    onload_functions.push('apply_onkeypress_event()');
    

// ]]>

Solution

The modern way to attach onLoad and onUnload handlers is to use window.addEventListener('load', onloadFunction) and window.addEventListener('unload', onunloadFunction). Unlike window.onload, you can attach multiple handlers, saving you the work of writing your own support for multiple handlers.

Unfortunately, Internet Explorer only added support for addEventListener() in IE 9. Therefore, you'll probably need a shim. That would be a common shim that supports a standard interface, rather than your own hack, so I would still consider it an improvement.

Context

StackExchange Code Review Q#40674, answer score: 8

Revisions (0)

No revisions yet.