Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Attaching to Particular Form Submissions

by erroneousBollock (Curate)
on Nov 28, 2007 at 06:28 UTC ( #653489=note: print w/replies, xml ) Need Help??

in reply to Attaching to Particular Form Submissions

if (window.addEventListener) { window.addEventListener("load",setupEvents,false); } else if (window.attachEvent) { window.attachEvent("onload", setupEvents); } else { window.onload=setupEvents; }
Great, you're attaching a handler to the 'load' event non-destructively, but then...

function setupEvents(evnt) { document.forms[1].onsubmit=myAlert; } function myAlert() { alert("gackerriffic"); }
... you're destructively assigning to document.forms[1].onsubmit (clobbering any event handler that may already be present).

A better idea is to use the following:

var isIE = /*@cc_on!@*/false; // is the browser Internet Explorer +? var addEvent = !isIE ? function (obj, type, fn) { obj.addEventListener(type, fn, fals +e); } : function (obj, type, fn) { obj['e'+type+fn] = fn; obj[type+fn] = function(){ obj['e'+type+fn](window.event); } +; obj.attachEvent('on'+type,obj[type+fn]); }; addEvent(window,'load',function(evt) { addEvent(this.document.forms[1],'submit',function(evt) { alert("gackerriffic"); }); });

(Any browser that implement neither addEventListener nor attachEvent is not worth supporting... the case for IE is already marginal ;)

If you must clobber a node's event handler 'slot' (eg: for reasons of event ordering) consider assigning an event handler that references the existing handler.

myNode.onevent = function() { // your code if (myNode.onevent),arguments); };

I want to target form submissions of a particular type, which is not always the second form.

I'm thinking I want to request that the forms on perlmonks be given names or id attributes so I can pick them out easily. Is there another way?

Each of the different kinds of page (usually) has a seperate nodeid (can be parsed from document.location.href).

As an example, the 'Comment On' page has node_id=3333 and takes a get parameter of the form parent=123456 to refer to the node being commented upon.

As mentioned above, you can also look for certain things in the document.body.innerHTML.

As an example, once you've submitted the 'Comment On' page (via preview) you can no-longer parse the parent nodeid from the document.location.href. It can be found in the main form of the (preview) page in a hidden input named 'note_parent_node'.

If XPath is available (either natively, or via a selector library like jQuery), I highly recommend it over using regular expressions to parse the contents of the pages.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://653489]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-02-20 13:26 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (296 votes). Check out past polls.