Get the URL generated from a form on submit with JavaScript

This is possible and easy with the objects URLSearchParams and FormData.

FormData is an object representation of a form, for using with the fetch API. It can be constructed from an existing element like this:

let form = document.forms[0];
let formData = new FormData(form);

Then comes the URLSearchParams object, which can be used to build up query strings:

let search = new URLSearchParams(formData);

and now all you need to do is call the toString function on the search object:

let queryString = search.toString();

Done!


To put it simply, you can't. The best you can do is to collect the form field values yourself, or using jQuery's .serialize() function, which returns those values exactly as you'd expect:

name=value&name2=value2

If you mean getting the form's action URL, that URL can be retrieved like this:

document.getElementById("form-id").action

If you are using jQuery and assuming you are doing an Ajax request, it would be like this:

var el = $('#form-id');
$.ajax({
    type: el.attr('method'),
    url: el.attr('action'),
    data: el.serialize(),
    context: this
}).done(callback);