Values returned by Javascript location search property can be modified using javascript substring, indexof functions and string array. You can extract the values of each query string variable and value returned in the form of string by location search property. Substring function with start index value as 1 removes the “?” question mark value from the beginning of location search value retrieved. Further you can split the string from “&” sign and then extract the each parameter and its value based on the index of “=” equal to sign.

Example for Javascript Location Search Substring

<html>
<head>
    <title>Javascript Location Search</title>

<script type="text/javascript" language="javascript">
    function LocationSearch() {
        var queryString = new String();
        queryString = window.location.search;
        queryString = queryString.substring(1);
        document.getElementById("div1").innerHTML = queryString + "<br />";
        
        var arr1 = new Array();
        arr1 = queryString.split("&");

        var arr2 = new Array();

        for (var i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i].toString().substring(
                            arr1[i].toString().indexOf("=") + 1
                            );

            arr1[i] = arr1[i].toString().substring(
                                0,
                                arr1[i].toString().indexOf("=") 
                                );
        }

        document.getElementById("div1").innerHTML +=
                                                  arr1.concat() + "<br />";

        document.getElementById("div1").innerHTML += 
                                                  arr2.concat();
    }

    function setLocationSearch(valueObj) {
        window.location.search = valueObj;
    }
</script>

</head>
<body>
    <div id="div1">
    </div>
    <br />
    <br />
    <input type="button" 
            id="btn" 
            value="Location Search Substrings" 
            onclick="LocationSearch();" />
</body>
</html>

Assume that current URL for above example consists of location search query string parameters are: 

?q1=value1&q2=value2&q3=value3

Above example will display the output as follows onclick event of button control:

q1=value1&q2=value2&q3=value3

q1,q2,q3

value1,value2,value3 

This example code shows how you can use the Javascript substring, indexof functions and location.search property to get and set the query string values.

Continue to next tutorial: Javascript Window Location Object to learn window location object porperties and methods

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.