Question: In $.ajax there is beforeSend function, but now I'm trying to use XMLHttpRequest, I'm looking for equivalent function of beforeSend in $.ajax. How can i implement it in here.

Here is my xhr code,

```
xhr = new XMLHttpRequest();
    var url = '../ajax/ajax_edit/update_ajax_staffUser.php';

    if(file.files.length !== 0){

        if(!check(fileUpload.type)){
            alert("This file format not accepted");
            return false;
        }
        xhr.open('post', url+param, true);
        xhr.setRequestHeader('Content-Type','multipart/form-data');
        xhr.setRequestHeader('X-File-Name', fileUpload.name);
        xhr.setRequestHeader('X-File-Size', fileUpload.size);
        xhr.setRequestHeader('X-File-Type', fileUpload.type);
        xhr.send(fileUpload);
    }else{
        xhr.open('post', url+param, true);
        xhr.setRequestHeader('Content-Type','multipart/form-data');
        xhr.send(fileUpload);
    }

    xhr.onreadystatechange = function(e){
        if(xhr.readyState===4){
            if(xhr.status==200){
                $('.bounce_dim').show();
                setTimeout(function(){
                    $('.trigger_danger_alert_changable_success').show().delay(5000).fadeOut();
                    $('#palitan_ng_text_success').html('User successfully modified');
                    $('#frm_edit_staffUser')[0].reset();
                    $('#modal_staff').modal('hide');
                    $('.bounce_dim').hide();
                },1000);
                getUserStaffTable();
            }
        }
    }

```

Since the users are uploading image to my web, I need to make a waiting interface before fires the call since the image size are too large.

Answer: 
You can do this by just putting the *beforeSend()* function before your XHR intantiation, like this:

```
beforeSend();
xhr = new XMLHttpRequest();

```

But you should define your *beforeSend()* function before the code above:

```
var beforeSend = function(){
    // your code here
}

```
`.beforeSend` just calls the function before running `.send`, so just put your code before the line:

```
xhr.setRequestHeader('Content-Type','multipart/form-data');
xhr.setRequestHeader('X-File-Name', fileUpload.name);
xhr.setRequestHeader('X-File-Size', fileUpload.size);
xhr.setRequestHeader('X-File-Type', fileUpload.type);
beforeSend(); // Put any code to run before sending here
xhr.send(fileUpload);

```