title: How to edit part of anchor in URL
body: <p>I have code for get part of anchor:</p>
  
  <pre><code>function _get_part(queryString, name) {
    var match = '&amp;' + name + '=';
    var i = queryString.indexOf(match);
  
    if(i &lt; 0) {
      match = name + '=';
      if(queryString.slice(0, match.length) == match)
        i = 0;
    }
  
    if(i &gt; -1) {
      i += match.length;
      return queryString.slice(i, queryString.indexOf('&amp;', i) &gt;&gt;&gt; 0);
    }
  };
  
  function get_location_hash() {
      return window.location.hash.substr(2);
  }
  
  function get_part(name) {
      return _get_part(get_location_hash(), name);
  }
  </code></pre>
  
  <p>I need a function for changing a part of the anchor, if this part exists, or add a part if it does not exist.</p>
  
  <p>At this time I use the following code:</p>
  
  <pre><code>function set_part(queryString, key, value) {
    var oldv = key + '=' + get_part(key);
    var newv = key + '=' + value;
    window.location.hash = '/' + queryString.replace(oldv, newv);
  }
  </code></pre>
  
  <p>But if the part of the anchor does not exist, the anchor doesn't change.</p>
  
  <p>URL format: ...page/#/var1=blablabla&amp;var2=var2text&amp;gghh=edere</p>
  
  <p>Anchor - #/var1=blablabla&amp;var2=var2text&amp;gghh=edere</p>
  
  <p>Sorry about my English.</p>
  
  <p>Thanks a lot!</p>
  
  <p><strong>update:</strong> </p>
  
  <p>it awesome, thank you very much!
  only one problem: i load page withoud any anchors:
  .../page/
  nex use this code:</p>
  
  <pre><code>set_part(get_location_hash(), 'filter', 'data');
  set_part(get_location_hash(), 'filter2', 'data2');
  set_part(get_location_hash(), 'fdgfg', 'fdgfdg');
  alert(get_part('fdgfg'));
  </code></pre>
  
  <p>and receive .../page/#/=&amp;filter=data&amp;filter2=data2&amp;fdgfg=fdgfdg</p>
  
  <p>how to delete first '=' symbol?</p>