1

I need help because I am trying to create a lib with javascript in order to create, modify, delete params in my url, I explain :

www.mydomain.com/thing/?id=1&et=67&type=chercher

How u can see, my params are random, it's dynamic, It's not every time the same url, I had this url, one time I can have ?id=1&et=67&type=chercher and other time I can have ?id=1&type=chercher or for example just ?id=1 or others params.

So it's not easy because of user action, the url with params will change, so is there an lib or an application written with javascript which can do this easily ?

Thx everyOne for your futures responses !!!!

Necko
  • 179
  • 2
  • 12
  • And what are you trying to do with these parameters? And by the sounds of your question it seems you want a library to let you write the library you want to create..? – David says reinstate Monica Jul 04 '12 at 08:56
  • It's for a web application. Thoses params are filters in order to change a main div front of my web page – Necko Jul 04 '12 at 08:57
  • 1
    https://github.com/allmarkedup/jQuery-URL-Parser – Alexander Beletsky Jul 04 '12 at 08:57
  • Obiously I want find a lib, not create it, if it's exist – Necko Jul 04 '12 at 08:58
  • 1
    Similar Question: http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript – starbeamrainbowlabs Jul 04 '12 at 09:00
  • 1
    try this http://medialize.github.com/URI.js/ – Ankur Loriya Jul 04 '12 at 09:08
  • I have a question, The jquery plugin cannot modify the url ? it can just get params or segment of the url ? it's true ? if yes, That not enough for me, I need to have a lib that can modify the url params – Necko Jul 04 '12 at 09:11
  • to starbeamrainbowlabs : It's not the same sorry, because i need to modify the url, not JUST GET PARAMS FROM IT thx to read with attention that I said, thx – Necko Jul 04 '12 at 09:14
  • In that case have a look at [URI.js](http://medialize.github.com/URI.js/). It was built for *modifying* URLs as painlessly as possible. – rodneyrehm Jul 04 '12 at 09:27
  • Yes, http://medialize.github.com/URI.js/ seems very good for me – Necko Jul 04 '12 at 09:30
  • Thx to all, But there is a problem. My application is an explorer. The user can check and unckeck some filters in order to improve his search. I don't want the page reload. I thought that JS functions like document.location.href can be modified whithout refreshing the page. But no. I am obliged to user the caracter anchor '#' in order to push in my url the filters. It's the good way ? For resume i am looking for a solution where i can easily push and remove params in my url without refresh the page. – Necko Jul 05 '12 at 09:42

3 Answers3

3

You are creating a library to manipulate URLs? Have you had a look at the existing solutions?

URI.js is something I can recommend. In its Readme URI.js links to a bunch of alternative solutions you could look into as well.

rodneyrehm
  • 12,971
  • 37
  • 56
1

Yes, absolutely. You can use the jQuery URL Parser plugin. Just make sure you include also jQuery in your page and not only the plugin.

Nadav S.
  • 2,407
  • 2
  • 22
  • 37
  • jQuery _is_ JavaScript... it's not only based on JavaScript, it's written in it. In fact, jQuery is nothing more than a huge JavaScript object, that is constructed in a self invoking function. – Elias Van Ootegem Jul 04 '12 at 09:04
  • 1
    Oh, you think I didn't know it? Maybe I have a mistake with "Javascript only", that's true. But defenetly, **I know that jQuery is a Javascript library** – Nadav S. Jul 04 '12 at 09:06
1

Do u need library which manipulate URL string? pass associative array(HASH) to function {a:2, b:3, c:4} and then go through this hash and form string.

Is this what u need?

To update - split string by & and downparse into HASH, and replace value by H['a'] = 4;

To delete - delete H["a"];

But better do not reinvent bicycle use tested solutions: http://code.google.com/p/jsuri/

Boris Ivanov
  • 3,863
  • 1
  • 27
  • 36
  • for example, I need a function which can add params in my url, so for example we can have this prototype : function add_param_in_url(key, value); function remove_param_in_url(key); function get_value_by_key(key); – Necko Jul 04 '12 at 09:18
  • Hey, it's a cool lib but the function delete doesn't work it's very boring ! look at this example http://mydomain.com/expositions?q=books&domaine=1&domaine=2 when I call deleteQueryParam with the key 'domain' and the value '1' all the params domain are deleted .... I don't understand why that do that, an idea ? – Necko Jul 04 '12 at 10:34
  • An other question, how I can update the Url after having modify it ? – Necko Jul 04 '12 at 10:38
  • U write it does not work. Why do u need 2 parameters with same name anyway? one overwrite another when server side script will parse it. – Boris Ivanov Jul 04 '12 at 11:50
  • U right about delete function. I already bug reported to authors of lib. Regarding update after modify - what do u mean? For me update and modify synonyms. – Boris Ivanov Jul 04 '12 at 15:55
  • »U write it does not work. Why do u need 2 parameters with same name anyway? one overwrite another when server side script will parse it« that is, spoken globally, not true. PHP will do that, yes. ruby for example won't. – rodneyrehm Jul 04 '12 at 20:03
  • Your provided link is outdated. – robsch Aug 10 '18 at 06:20