1

We have a SPA html5 web page that is used in the mobiles. As the detail page is set after the #, I would like to change the url that is sent when an user clicks in the share button to something more informative before the #. That way I change the descriptions, titles, and so on...

Example of what I want:

Url in the browser: url/#id=1

Url sent to the "share": url?id=1

Do you now any way to do it? But I don't want to make another request to the server.

Thanks in advance.

Pablo Castilla
  • 2,723
  • 2
  • 28
  • 33
  • 1
    HI, https://developer.mozilla.org/en-US/docs/Web/API/document.location - throw `var _id = document.location.hash; alert(_id);` in to your code. Do you have what you need ? – Rob Sedgwick Mar 29 '14 at 10:45
  • I want to modify it just for the button, not in the browser url place because that would fire another request to the server. – Pablo Castilla Mar 29 '14 at 17:36
  • It has been asked and well answered here: http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page – Pablo Castilla Mar 30 '14 at 07:38
  • Ok, then you didn't asked properly. You asked for modified url passed only to share buttons. – Miljan Puzović Mar 30 '14 at 10:28

1 Answers1

1

I hope that I understand what you want: you want to catch current url and modify it, right? If so, then do next:

Open this js fiddle http://jsfiddle.net/6Yf3r/.

Fiddle code is

$(document).ready(function(){
    var currentUrl = window.location.href;
    var newUrl = currentUrl.replace('/_','?');
    alert(currentUrl);
    alert(newUrl);
});

Ignore first two alerts, and then click run. See what happens with url's in those two alerts? You can now use this code, and modify it to .replace('/#','?');. This function will find the part /# in your URL and replace it with ?. Now you will have desired URL as a string and just use that new url in right place with your share button.

Miljan Puzović
  • 5,840
  • 1
  • 24
  • 30
  • thanks for the answer. But I have a question... that would send another request to the server, wouldn't it? I don't want that – Pablo Castilla Mar 29 '14 at 17:31
  • There is no another request to the server. Once page is loaded, server may rest. On click event, URL will get #someid prefix, and then you will with JavaScript/jQuery do the rest: get current url, treat it like a string, replace some characters with another one, an pass that new string to the share buttons. Open console and you will se all server requests. – Miljan Puzović Mar 29 '14 at 19:02