5

Say I want to overwrite form.elements on all forms (current and future).

Motivation

This question on polyfilling RadioNodeList could be solved by intercepting all form elements and manually writing an own getter for the elements property on them.

How do I intercept?

Apart from the below what other ways are there of creating (form) elements that I need to intercept

  • document.createElement
  • document.createElementNS
  • Node.importNode
  • Node.cloneNode
  • DOMImplementation.createDocument (Maybe, depends how effective overwriting Document.prototype.createElement/NS is)

What are the other downsides?

Seperate question, ignoring the performance hits what good reasons are there for not intercepting node creation?

Community
  • 1
  • 1
Raynos
  • 166,823
  • 56
  • 351
  • 396

1 Answers1

0

The interception of the form elements can be done with a function that parses the nodes by tag name searching for form if a specific event or attribute is not set like you have than you would have to set it else it is an already intercepted form.

The second alternative is to override the createElement on Element and on document for the new elements to come to be parsed by the above mentioned function.

The bad part is that for older IE versions you would have to write a separate HTC file to make the Element's behavior as you wish.

Hope this helps, even if it is a lot of info and no code ( it is a pretty nasty code ).

EDIT:

In other words you could create a Overridden DOM class for Cross-Browser usage ( or at least this is what i am doing for the las 4 months ), where you take all the special behaviors and treat them for special reasons.

khael
  • 2,600
  • 1
  • 15
  • 36
  • how do you deal with new nodes not having the behaviour. And how do you gaurd againts `innerHTML` and `document.write` etc – Raynos Jan 20 '12 at 15:38
  • either you create them using the newly overridden createElement either you set a watcher on innerHTML or by manualy appending a call to the form parser function every time you make a change. If you do it by ajax just call it at the start of the update handler function. – khael Jan 20 '12 at 15:41
  • @Raynos document.write can be overridden also to call your function. – khael Jan 20 '12 at 15:42
  • You mention "overridden DOM class for cross-browser usage". Is it DOM compliant? Does it fix browser bugs? Is it open source? Is it similar to [DOM-shim](https://github.com/Raynos/DOM-shim) ? – Raynos Jan 23 '12 at 18:09
  • @Raynos Yes, Yes, Not yet, I'll take a closer look to DOM-shim but at first sight is similar. – khael Jan 23 '12 at 22:52
  • I will bribe you with SO rep if you open source and reference it o/ – Raynos Jan 23 '12 at 22:54
  • @Raynos :)) nice but i can't accept it. First i want to remodel it and then i will publish it. I want to be a serious thing even though i already am using it in some nasty ajax-based web-apps. But when finished you'll be the first to know. ;) – khael Jan 23 '12 at 22:57