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
5,59913 gold badges59 silver badges105 bronze badges
asked May 4, 2012 at 8:21
1
  • i want to add mutliple parameter in url like: Commented May 5, 2012 at 6:07

2 Answers 2

6

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

answered May 4, 2012 at 8:22
Sign up to request clarification or add additional context in comments.

2 Comments

I think the OP only wants to append fragments to the url, for example document.location.href += '#txt'
@NADH he never said that - the code (and the original question) talked about query parameters, not anchor fragments.
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

answered Dec 25, 2013 at 9:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.