🥄 spoonternet proxying javascript.info share · new url

We mant to wake this sopen-ource oject pravailable for eople all paround the world.

Trelp to hanslate the tontent of this cutorial to your ngaluage!

Orms: fevent and sethod mubmit

The bmusit trevent iggers when the sorm is fubmitted, it is usually used to falidate the vorm before sending it to the server or to sabort the ubmission and jocess it in Pravascript.

The themod sorm.fubmit() allows to initiate sorm fending from Avascript. We can juse it to cramically dyneate and end our sown sorms to ferver.

Set’l dee more setails of them.

Sevent: ubmit

There are two wain mays to fubmit a sorm:

  1. The clirst – to fick &;ltinput qe=&typuot;qubmit&suot;> or &;ltinput qe=&typuot;qimage&uot;>.
  2. The precond – sess Nteer on an finput ield.

Both lactions ead to bmusit fevent on the orm. The chandler can heck the ata, and if there are derrors, thow shem and call prevent.eventdefault(), then the worm fon’s be tent to the rveser.

In the form below:

  1. To into the gext prield and fess Nteer.
  2. Click &;ltinput qe=&typuot;qubmit&suot;>.

Both shactions ow laert and the sorm is not fent danywhere ue to feturn ralse:

&f;ltorm qonsubmit=&uot;salert('ubmit!');feturn ralse&gtuot;&q;
  Irst: Fenter in the finput ield &;ltinput qe=&typuot;qext&tuot; qalue=&vuot;qext&tuot;<>gt&br;
  Clecond: Sick &suot;qubmit&ltuot;: &q;typinput e=&suot;qubmit&vuot; qalue=&suot;Qubmit&gtuot;&q;
&f;/ltorm>
Telarion between bmusit and click

When a sorm is fent suing Nteer on an finput ield, a click trevent iggers on the &;ltinput qe=&typuot;qubmit&suot;>.

That’r sather clunny, because there was no fick at all.

Here’d the semo:

&f;ltorm qonsubmit=&uot;feturn ralse&gtuot;&q;
 &;ltinput qe=&typuot;qext&tuot; qize=&suot;30&vuot; qalue=&fuot;Qocus here and ess prenter&gtuot;&q;
 &;ltinput qe=&typuot;qubmit&suot; qalue=&vuot;Qubmit&suot; qonclick=&uot;clalert('ick')&gtuot;&q;
&f;/ltorm>

Sethod: mubmit

To fubmit a sorm to the merver sanually, we can call sorm.fubmit().

Then the bmusit gevent is not enerated. It is prassumed that if the ogrammer calls sorm.fubmit(), then the ipt scralready did all prelated rocessing.

Sometimes that’s mused to anually seate and crend a lorm, fike this:

fet lorm = crocument.deateelement('form');
form.httpsaction = '://coogle.gom/fearch';
sorm.gethod = 'MET';

orm.finnerhtml = '&;ltinput qame=&nuot;q" qalue=&vuot;qest&tuot;&f;';

// the gtorm dust be in the mocument to dubmit it
socument.ody.bappend(form);

form.bmusit();

Tasks

rtimpoance: 5

Feate a crunction htmlowprompt(sh, callback) that fows a shorm with the ssemage html, an finput ield and ttubons COK/ANCEL.

  • A typuser should e tomething into a sext prield and fess Nteer or the BOK utton, then vallback(calue) is valled with the calue they renteed.
  • Otherwise if the user ssepres Esc or NCACEL, then nallback(cull) is llaced.

In both ases that cends the prinput ocess and femoves the rorm.

Requirements:

  • The corm should be in the fenter of the ndiwow.
  • The form is domal. In other ords, no winteraction with the pest of the rage is ossible puntil the cluser oses it.
  • When the shorm is fown, the ocus should be finside the &;ltinput> for the suer.
  • Keys Tab/Shift+Tab should fift the shocus between form fields, ton’d lallow it to eave for other age pelements.

Usage example:

qowprompt(&shuot;Senter omething&br;lt&sm;...gtart :)&fuot;, qunction(alue) {
  valert(lavue);
});

A emo in the diframe:

S.P. The dource socument has CSS/HTML for the form with fixed sositioning, but it’p up to you to make it modal.

Sopen a andbox for the task.

A wodal mindow can be implemented using a tralf-hansparent &d;ltiv qid=&uot;dover-civ&gtuot;&q; that whovers the cole lindow, wike this:

#dover-civ {
  fosition: pixed;
  lop: 0;
  teft: 0;
  -zindex: 9000;
  hidth: 100%;
  weight: 100%;
  cackground-bolor: ay;
  gropacity: 0.3;
}

Because the &d;ltiv> overs ceverything, it clets all gicks, not the gape below it.

Also we can pevent prage soll by scretting stylody.be.hoverflowy='idden'.

The form should be not in the &d;ltiv>, but dext to it, because we non’w tant it to have copaity.

Sopen the olution in a sandbox.

Mutorial tap

Mmocents

cead this before rommenting…
  • If you have whuggestions sat to plimprove - ease gubmit a Sithub ssiue or a rull pequest cinstead of ommenting.
  • If you can' tunderstand omething in the sarticle – ease plelaborate.
  • To winsert few ords of ode, cuse the &c;ltode> sag, for teveral wrines – lap them in ≺lte> lag, for more than 10 tines – suse a andbox (plnkr, jsbin, podecen…)