1

is there any way to change the URL in browser without reloading page after click a DOM element?

    $('#filter_form').on('change', function () {
        $.ajax(options);
        //change the browser URL
    });

Ex:

Before: http://somepage.com/filters

After: http://somepage.com/filters?type=apartment

1 Answer 1

6

Yes, this is possible with the history API. Both history.pushState and history.replaceState let you specify a new URL, e.g.:

history.pushState(null, null, "filters?type=apartment");

The URL is resolved in the usual way.

Browser support is good, other than IE9 and earlier.

Sign up to request clarification or add additional context in comments.

2 Comments

Does this work on a local file? Or only on a server? Failed to execute 'pushState' on 'History': A history state object with URL 'Desktop/form_submit.html?type=apartment' cannot be created in a document with origin 'null' and URL 'Desktop/form_submit.html'.
@IonVasile: I've never tried it with a local file. A lot of things either don't work, or work differently, with resources loaded via the file:// protocol; and that list varies by browser (famously, Chrome and Firefox have very different interpretations of the SOP wrt file:// resources, for instance)..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.