-2

Remove the hash # from url (without redirect)

location.hash = "?userid=" + document.getElementById("userid").innerHTML;
location.href.replace('#', '');

EDIT: Answer = window.history.pushState("", "", window.location.href + "?userid=" + document.getElementById("userid").innerHTML);

Brigitte
  • 79
  • 2
  • 9
  • 5
    .replace() returns the adjusted string. So at the very least, you would need to do something like `location.href = location.href.replace( '#', '' );` – Patrick Moore Jul 30 '16 at 18:40
  • I had tried that already but it send it int an infinite loop – Brigitte Jul 30 '16 at 18:42
  • What?? What infinite loop? – nicael Jul 30 '16 at 18:42
  • simply `if(location.href.indexOf('?userid='==-1) location.href += "?userid=" + document.getElementById("userid").innerHTML` – Pranav C Balan Jul 30 '16 at 18:44
  • Thank you i will try this now – Brigitte Jul 30 '16 at 18:44
  • If you're just trying to redirect to another page, it makes no sense to first add the hash with `location.hash` and then trying to remove it. – JJJ Jul 30 '16 at 18:46
  • im not trying to redirect – Brigitte Jul 30 '16 at 18:48
  • im just trying to add to the end of the url for affiliate reasons – Brigitte Jul 30 '16 at 18:49
  • You can't add stuff to the URL without redirecting using this method. – JJJ Jul 30 '16 at 18:50
  • Well can you help me, i tried substr(1) on the location hash but that didnt work. – Brigitte Jul 30 '16 at 18:51
  • @Juhana : as per her code it will continuously redirect the page, since script executes on page load – Pranav C Balan Jul 30 '16 at 18:51
  • @PranavCBalan Of course it does. That's what I've been saying. Did you reply to the wrong person? (Brigitte is a female name, btw.) – JJJ Jul 30 '16 at 18:52
  • @Brigitte Google for "change url without redirect". – JJJ Jul 30 '16 at 18:52
  • Thanks all, please take back your down votes :( window.history.pushState("", "", window.location.href + "?userid=" + document.getElementById("userid").innerHTML); – Brigitte Jul 30 '16 at 18:59
  • 1
    @PranavCBalan How are they not duplicates? [Replace method doesn't work](http://stackoverflow.com/q/1433212/1529630) explains why `replace` without assignment does nothing. And [Remove hash from url](http://stackoverflow.com/q/4508574/1529630) explains how to achieve what the OP wants. Her answer here is a corollary of the top answer in there. – Oriol Jul 30 '16 at 23:35

1 Answers1

2
window.history.pushState("", "", window.location.href +  "?userid=" + document.getElementById("userid").innerHTML);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Brigitte
  • 79
  • 2
  • 9