4

I have a url that looks like this

mysite.com/index.php?page=home&gender=female&age=22

or

mysite.com/index.php?page=home&gender=female&occupation=programmer

How can I append a new parram orderby=value and order=asc using a tag like how the submit button works.

like when I click the link Name It will add new param on the link a I can sort the data by it

<a href="#">Name</a> | <a href="#">Age</a>

like how wordpresshandles it

wordpress/wp-admin/users.php?orderby=login&order=desc
Shikiryu
  • 9,920
  • 6
  • 47
  • 74
Cindy93
  • 970
  • 1
  • 8
  • 24

4 Answers4

4

Why don't you just build the URLs during the template render? No need for JS

eg

<a href="/index.php?occupation=programmer&orderby=name&order=desc">Name</a>

Just check the current order value when you render and invert it.

Obviously if you're using AJAX this is less useful.

kinghfb
  • 976
  • 4
  • 14
3

You can use:

document.URL + "orderby=desc"

The only problem is that when you click again you get orderby twice. Therefore you should check if the url already contains the orderby parameter.

A more complex function to set or replace url parameters could be found here:

Add / Change parameter of URL and redirect to the new URL

Community
  • 1
  • 1
Bas van Dijk
  • 7,931
  • 8
  • 51
  • 81
2

use it

var url = document.URL;
if(url.indexOf('orderby') == -1){
url = document.URL + "orderby=desc";
}
0

You could use the function posted here https://stackoverflow.com/a/6021027/1021726. All credit goes to amateur.

The function either creates a new value on the query string or updates an existing value.

Add an onclick event on the <a> tag and call the function like

updateQueryStringParameter(window.url, "orderby", "myName");
updateQueryStringParameter(window.url, "order", "desc");
Community
  • 1
  • 1
user1021726
  • 608
  • 9
  • 21
  • @Cindy93 - thats another solution to the problem and without knowing how your application looks like it's difficult for any of us to know what would be best. – user1021726 Dec 06 '13 at 07:45