Often students ask me: “Which element of the site is the most important?”, To which I answer them - forms. After all, with the help of forms, users perform almost all conversion actions. It is with this element that most problems are associated. In this article I will try to tell you what can be improved when interacting with forms. And at the same time to describe the new features of working with them in browsers.required , autofocus , placeholder .required - required field;autofocus - setting the focus on the field when loading the page;placeholder - field description.date , email , number , range and others. However, the most innocuous of them ( email ) is still used with caution. But in order for it to earn special action is not necessary. Browsers that do not know this type of field will consider it text.E:valid , E:invalid , E:required - with which you can describe the style design of fields in different situations.localStorage as you type.
- if ( window. localStorage ) {
- var elements = document. querySelectorAll ( '[name]' ) ;
- for ( var i = 0 , length = elements. length ; i < length ; i ++ ) {
- ( function ( element ) {
- var name = element. getAttribute ( 'name' ) ;
- element. value = localStorage. getItem ( name ) || '' ;
- element. onkeyup = function ( ) {
- localStorage. setItem ( name , element. value ) ;
- } ;
- } ) ( elements [ i ] ) ;
- }
- }
required attributes, we can also do the validation in a new way. In the HTML5 specification for the form element, the checkValidity() method has been added. It returns true or false . The form's working strategy will be very simple: if the validation check gives a negative result, we block sending the form, otherwise, we allow sending.
- submit. disabled = ! form. checkValidity ( ) ;
FormData object.
- form. onsubmit = function ( event ) {
- if ( window. FormData ) {
- event. preventDefault ( ) ;
- var data = new FormData ( form ) ;
- var xhr = new XMLHttpRequest ( ) ;
- var url = form. getAttribute ( 'action' ) + '? time =' + ( new Date ( ) ) . getTime ( ) ;
- xhr. open ( 'post' , url ) ;
- xhr. onreadystatechange = function ( ) {
- if ( xhr. readyState == 4 && xhr. status == 200 ) {
- // server response: xhr.responseText
- }
- } ;
- xhr. send ( data ) ;
- }
- } ;
xhr.responseText ), but this will change over time. For example, if we know for sure that the answer is JSON, we can get a JavaScript object right away.
- var xhr = new XMLHttpRequest ( ) ;
- xhr. open ( method , url ) ;
- xhr. responseType = 'json' ;
- xhr. onreadystatechange = function ( ) {
- if ( xhr. readyState == 4 && xhr. status == 200 ) {
- // server response: xhr.response
- }
- } ;
- xhr. send ( ) ;
xhr.response property. And the xhr.responseType property can take other values, for example: arraybuffer , blob , document .localStorage and clear the rest. Thus, if the user wants to submit the form again, some of the information will already be filled out.multiple - allows you to select multiple files;accept - allows you to specify which files to select.
- < input type = "file" name = "image" accept = "image / * multiple " >
FileReader object from the File API specification.
- document. querySelector ( '[type = file]' ) . addEventListener ( 'change' , function ( ) {
- [ ] . forEach . call ( this . files , function ( file ) {
- if ( file. type . match ( /image.*/ ) ) {
- var reader = new FileReader ( ) ;
- reader. onload = function ( event ) {
- var img = document. createElement ( 'img' ) ;
- img. src = event. target . result ;
- div. appendChild ( img ) ;
- queue. push ( { file : file , element : img } ) ;
- } ;
- reader. readAsDataURL ( file ) ;
- }
- } ) ;
- } , false ) ;

queue . Earlier in the article we used the FormData object, now we just add a list of files to it.
- var data = new FormData ( form ) ;
- queue. forEach ( function ( element ) {
- data. append ( 'image' , element. file ) ;
- } ) ;
- function previewImages ( files ) {
- [ ] . forEach . call ( files , function ( file ) {
- if ( file. type . match ( /image.*/ ) ) {
- var reader = new FileReader ( ) ;
- reader. onload = function ( event ) {
- var img = document. createElement ( 'img' ) ;
- img. src = event. target . result ;
- div. appendChild ( img ) ;
- queue. push ( { file : file , element : img } ) ;
- } ;
- reader. readAsDataURL ( file ) ;
- }
- } ) ;
- }
wrapper class will be a zone for moving files. Add events for it.
- var file = document. querySelector ( '[type = file]' ) ;
- var dropzone = document. querySelector ( '.wrapper' ) ;
- file. addEventListener ( 'change' , function ( ) {
- previewImages ( this . files ) ;
- this . value = '' ;
- } , false ) ;
- dropzone. addEventListener ( 'dragover' , function ( event ) {
- event. preventDefault ( ) ;
- dropzone. classList . add ( 'active' ) ;
- event. dataTransfer . dropEffect = 'copy' ;
- } , false ) ;
- dropzone. addEventListener ( 'drop' , function ( event ) {
- event. preventDefault ( ) ;
- dropzone. classList . remove ( 'active' ) ;
- previewImages ( event. dataTransfer . files ) ;
- } , false ) ;
dragover ) and end ( drop ) events of moving files. We transfer all transferred files to previewImages . Thus, our form works in the same way with files selected through the site and moved from the computer.progress element, and you can take a div with a moving background. The process itself will occur in the progress event of the XMLHttpRequest specification.
- var xhr = new XMLHttpRequest ( ) ;
- xhr. upload addEventListener ( 'progress' , function ( event ) {
- if ( event. lengthComputable ) {
- progress. value = Math. round ( ( event. loaded * 100 ) / event. total ) ;
- }
- } , false ) ;

FileReader on iOS 6.Source: https://habr.com/ru/post/163739/
All Articles