How to remove parameters in URL using JavaScript

How to remove parameters in URL using JavaScript


JavaScript (“JS” for short) is a full-fledged dynamic programming language that, when applied to an HTML document, can provide dynamic interactivity on websites. 

In this tutorial we will show you How to remove parameters in URL using JavaScript. JavaScript  code snippet to remove the dynamic variables stored in the url as parameters, and check them as JavaScript variables ready for use with your scripts.

Usage

function removeParams(sParam)
{
            var url = window.location.href.split('?')[0]+'?';
            var sPageURL = decodeURIComponent(window.location.search.substring(1)),
                sURLVariables = sPageURL.split('&'),
                sParameterName,
                i;
         
            for (i = 0; i < sURLVariables.length; i++) {
                sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] != sParam) {
                    url = url + sParameterName[0] + '=' + sParameterName[1] + '&'
                }
            }
            return url.substring(0,url.length-1);
}

window.location = removeParams(parameter);

 

And this is how you can use this function assuming the URL is,

http://dummy.com/?technology=jquery&blog=jquerybyexample

window.location = removeParams('technology');

http://dummy.com/?blog=jquerybyexample

Share Your Comments