Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found