views:

7142

answers:

4

I have this URL: site.fwx?position=1&archiveid=5000&columns=5&rows=20&sorting=ModifiedTimeAsc

what I need is to be able to change the 'rows' url param value to something i specify, lets say 10. And if the 'rows' doesn't exist, I need to add it to the end of the url and add the value i've already specified (10).

Anyone know the easiest way to do this with jQuery?

+9  A: 

I think you want the query plugin.

E.g.:

window.location = jQuery.query.set("rows", 10);

This will work regardless of the current state of rows.

Matthew Flaschen
+1 really nice solution.. hadn't seen this before
Harry Lime
mofle
Mofle, that's because the Query plugin uses decodeURIComponent, and decodeURIComponent("%F8") is invalid.
Matthew Flaschen
How can I fix this? ;)
mofle
Get that weird stuff out of your URL. Or, in other words, "change your query-string to use only valid UTF-8 characters". :) F8 alone isn't a valid UTF-8 character.
Matthew Flaschen
Arnis L.
+5  A: 

Ben Alman has a good jquery querystring/url plugin here that allows you to manipulate the querystring easily.

As requested -

Goto his test page here

In firebug enter the following into the console

jQuery.queryString(window.location.href, 'a=3&newValue=100');

It will return you the following amended url string

http://benalman.com/code/test/js-jquery-url-querystring.html?a=3&b=Y&c=Z&newValue=100#n=1&o=2&p=3

Notice the a querystring value for a has changed from X to 3 and it has added the new value.

You can then use the new url string however you wish e.g using document.location = newUrl or change an anchor link etc

redsquare
Can you show me an example using this plugin to solve my problem?
mofle
updated my answer to show usage
redsquare
you made me do the above then went for the other one....sheesh!
redsquare
sorry, i actually clicked the wrong one ;)
mofle
Isn't it jQuery.param.querystring instead of jQuery.queryString ?
Petr Peller
+1  A: 

Would a viable alternative to String manipulation be to set up an html form and just modify the value of the rows element?

So, with html that is something like

<form id='myForm' target='site.fwx'>
    <input type='hidden' name='position' value='1'/>
    <input type='hidden' name='archiveid' value='5000'/>
    <input type='hidden' name='columns' value='5'/>
    <input type='hidden' name='rows' value='20'/>
    <input type='hidden' name='sorting' value='ModifiedTimeAsc'/>
</form>

With the following JavaScript to submit the form

var myForm = document.getElementById('myForm');
myForm.rows.value = yourNewValue;
myForm.submit();

Probably not suitable for all situations, but might be nicer than parsing the URL string.

Harry Lime
+4  A: 

you can do it via normal JS also

var newAdditionalURL = "";
var tempArray = url.split("?");
var baseURL = tempArray[0];
var aditionalURL = tempArray[1]; 
var temp = "";
if(aditionalURL)
{
var tempArray = aditionalURL.split("&");
for ( var i in tempArray ){
 if(tempArray[i].indexOf("rows") == -1){
   newAdditionalURL += temp+tempArray[i];
    temp = "&";
   }
  }
}
var rows_txt = temp+"rows=10";
var finalURL = baseURL+"?"+newAdditionalURL+rows_txt;
openidsujoy