Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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) myNode.onevent.call(myNode,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.

-David


In reply to Re: Attaching to Particular Form Submissions by erroneousBollock
in thread Attaching to Particular Form Submissions by andyford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (14)
    As of 2014-07-23 18:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (149 votes), past polls