Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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),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.


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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (8)
    As of 2018-05-25 11:14 GMT
    Find Nodes?
      Voting Booth?