1

I am using following code to append param to url. This is working fine but when parameter is appended in url, page is getting reloaded. I want to use this functionality without reloading the page .

function insertParam(key, value)
{
    key = escape(key); value = escape(value);

    var kvp = document.location.search.substr(1).split('&');


    var i=kvp.length; var x; while(i--) 
    {
        x = kvp[i].split('=');

        if (x[0]==key)
        {
                x[1] = value;
                kvp[i] = x.join('=');
                    alert('sdfadsf');
                break;
        }
    }

    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

    //this will reload the page, it's likely better to store this until finished
    document.location.search = kvp.join('&'); 
    //alert(document.location.href);

}

I want to add multiple params to url without reloading the page like:

  • txt1
  • txt2
  • txt3
  • link1
  • link2
  • link3

i want url : "..../search.php"

after click on txt2 i want url : "..../search.php#t_2"

after click on link2 i want url : "..../search.php#t_1&l_2"

The Dark Knight
  • 4,986
  • 10
  • 42
  • 86
Manoj Patil
  • 35
  • 1
  • 1
  • 9

2 Answers2

6

You can only do this using history.pushState(state, title, url) which is an HTML5 feature.

Alnitak
  • 313,276
  • 69
  • 379
  • 466
  • I think the OP only wants to append fragments to the url, for example `document.location.href += '#txt'` – Nadh May 05 '12 at 08:06
  • @NADH he never said that - the code (and the original question) talked about query parameters, not anchor fragments. – Alnitak May 05 '12 at 12:31
0

There is a new feature that aims to replace the use of location.hash with a better solution: pushState.

 window.history.pushState(data, "Title", "/new-url");

More information: http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate

Artem Kovalov
  • 904
  • 9
  • 10