html - Where to find "javascript" source code of V8 DOM method implementations (document.createElement())? -



html - Where to find "javascript" source code of V8 DOM method implementations (document.createElement())? -

this question has reply here:

hooking document.createelement using function prototype 2 answers

i need rewrite document.createelement() method , i'm looking javascript source code online ideas. searched http://code.google.com/p/v8/source/browse seems search returns results svn sources (even non-related libraries) makes messy. browsed code in svn, , there of course of study c++ source code, no javascript implementations.

you should not, , cannot, re-write native dom methods. methods such createelement "close surface" of client's emacscript implementation, have protected scopes, , not able reproduced "userland" script.

in case of createelement, might able overwrite createelement function of document object in browsers (see below), you'll find crashes , burns on existing clients/versions. further, cannot alter prototype of document because has none.

you can, however, easily , without risk create little wrapper function:

var createelement = function (ele_type, attrs) { var ele = document.createelement(ele_type); if (!ele) homecoming false; (var idx in attrs) { if ((idx == 'styles' || idx == 'style') && typeof attrs[idx] == 'object') { (var prop in attrs[idx]){ele.style[prop] = attrs[idx][prop]} } else if (idx == 'html') { ele.innerhtml = attrs[idx] } else { ele.setattribute(idx, attrs[idx]); } } // fire custom events or whatever needed }; var mynewdiv = createelement ('div', {'id':'mynewdiv', 'styles':{'color':'red'}});

that gives chance implement custom code upon creation of new element. work on browsers regardless of implementation differences, , future-proof other code write today.

another route mentioned above, not cross-browser or guaranteed work tomorrow, overwrite createelement function of active document instance. problem approach document object isn't defined specification -- implementation specific. today, firefox (for example) allow overwrite function, alter without notice.

document.createelement = function(originaldocumentcreate) { homecoming function() { var ele = originaldocumentcreate.apply(this, arguments); // fire custom events or whatever needed homecoming ele; }; }(document.createelement);

see also: http://stackoverflow.com/a/11727842/610573 (code concept credit esailija)

javascript html html5 dom v8

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -