Remove Query String from URL Using jQuery / Javascript

Share this Article
Reading Time: 3 minutes
341 Views

Inside this article we will see how to remove query string from url using jquery / javascript. If you are creating an application where you need to remove query string from url using jquery / javascript every time then this tutorial may help you.

Article is super easy to understand and also easy steps for implementation. There are several options and the way to remove query string from url for every developer may be different but using javascript functions may be same.

Example

Original URL

URL?blog=onlinewebtutor&name=sanjay

Converted URL will have no query string parameters.

URL

Learn More –

Let’s get started.


Create an Application

Create a folder with name query-string. Create a file index.html into it.

Open index.html and write this complete code into it.

<!DOCTYPE html>
<html lang="en">

<head>
    <title>How to Remove Query String from URL Using jQuery / Javascript</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <script type="text/javascript">
    
        $(document).ready(function() {

            var uri = window.location.toString();
            if (uri.indexOf("?") > 0) {
                var clean_uri = uri.substring(0, uri.indexOf("?"));
                window.history.replaceState({}, document.title, clean_uri);
            }
        });

    </script>
</body>

</html>

Application Testing

Open browser and type this –

URL – http://localhost/query-string/index.html?blog=onlinewebtutor&name=sanjay

Redirected URL

http://localhost/query-string/index.html

We hope this article helped you to learn how to remove query string from url using jquery / javascript Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Learn More about jQuery & Javascript Concept