html - IE10 - avoid form names in global scope for javascript -
i've got web application search box, , onkeypress event calls query.keypress()
. query defined in included javascript file. of time works correctly.
on ie10 screens, trying use search box gives error:
error: object doesn't support property or method 'keypress'
what have found of screens have query form name , id of 'query'. seems in ie10 appears in global scope, , overrides object literal javascript. info, search box not inside form.
i've replaced onkeypress code alert(query);
, see [object htmlformelement]
confirms happening.
does know how stop happening on ie10?
this problem can negated using own namespace, rather polluting global scope time , time again.
var my_stuff = {}; my_stuff.query = function () {}; my_stuff.my_const = 4; my_stuff.utilityfunction = function () {};
... way, breaks if add form element named my_stuff
, won't break if add form elements called query
, my_const
or utilityfunction
(not best names, gist).
like said, doesn't fix problem, negates it. polluting global scope bad practise.
you can, of course, not use global variable @ all, through closures. require lot of restructuring, if have application spread on multiple files, , no build process in place combine files.
(function () { var query = function () {}; // query.keypress() as want! }()); // note can't use outside of closure.
Comments
Post a Comment