简体   繁体   中英

Append parameter in url without reloading page

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"

您只能使用带有HTML5功能的history.pushState(state, title, url)来执行此操作。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM