Skip to main content
spelling in title
Link
Cœur
  • 39k
  • 25
  • 207
  • 282

jQuery append query parameters to current hash ommitingomitting duplicates

Source Link
Milen Mihalev
  • 350
  • 1
  • 7
  • 22

jQuery append query parameters to current hash ommiting duplicates

I have an url like http://example.com/#var1=1 I'm adding new query params to the hash using this function:

function appendHash(name, val){ var currentHash = location.hash; if(!currentHash){ location.hash = '#'+name+'='+val; }else{ location.hash = currentHash+'&'+name+'='+val; } }; 

However, this won't work as it should if the new name=val already exists in the current hash, e.g if current hash is #var1=1&var2=2 and I add var2=2 it will be appended again, and I don't want this to happen. What I need is to check if the new name and val already exist in the url and if exists skip it. Or maybe some other function will help?