user1001176
user1001176

Reputation: 1165

remove a variable from a link that comes in a url one or many times

i wanted to remove a variable and its value from ahref link like suppose the link has quantity=1 one time or two times then i want to remove it no matter how many times it comes in that link. what i had in my mind is to use the jquery replace . i made a code .

   var url = i.text.replace('&quantity','');

where i had the value

"https://example.com?pid=1&quantity=2&quantity=3"

but if i use replace i wont be able to remove the value of the variable. So what to do so that not only the string &quantity is removed but also the value associated with it is also removed like full &quantity=3. also keeping in mind that quantity and its values are dynamic it can be one time in the link with some other value or two times with some other.

Upvotes: 1

Views: 136

Answers (2)

DarkAjax
DarkAjax

Reputation: 16223

Try with something like:

var text = "https://example.com?pid=1&quantity=2&quantity=3";
var url = text.replace(/&quantity=\d+/g,'');

That'll replace the = sign and whatever number you have after, just make sure your variables are OK...

As you can see here: http://jsfiddle.net/darkajax/afXMN/

Upvotes: 1

Jayant Jt Hirani
Jayant Jt Hirani

Reputation: 5

You can use the $_post function rather than the get to hide this value if you wish or use a session

Upvotes: 0

Related Questions