-1

I want to add hash to my URL. For example

http://somesite.com/somesubdomain#p1=1&p2=2&p3=3

when I try to do this, all I get is:

http://somesite.com/somesubdomain#p1=1%23p2=2%23p3=3

So, in short, how to I add special characters in my URL hash.

EDIT:

I am using YUI browser history manager.

var hash = "p1=1&p2=2&p3=3"
YAHOO.util.History.navigate("state",hash);
hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
  • 1
    Provide the code you are trying to run. `window.location = 'http://somehost/' + '#p1=1&=p2=2'` works perfectly for me. Do you experience this in all browsers? – Dmitry Pashkevich Jun 04 '12 at 19:10
  • Url encode is what you are looking for. This is what you want. http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript – DarthVader Jun 04 '12 at 19:13
  • But doesn't YUI correctly decode your state back when you need it? – Dmitry Pashkevich Jun 04 '12 at 19:20
  • I cannot use window.location, neither is encodeURIComponent is useful in my case, may be I should look into YUI's History API. – hrishikeshp19 Jun 04 '12 at 19:20
  • @DmitryPashkevich: No it does not. It forces me to use multiple modules if I want to insert "&". So, no special character can be a part of hash string for YUI history manager. – hrishikeshp19 Jun 04 '12 at 19:23
  • If you want use framework methods, you should conform with the way developers intended it to be used (e.g. multiple modules, if I understood correctly). Otherwise use a third party router library or implement your own. – Dmitry Pashkevich Jun 04 '12 at 19:28

1 Answers1

0

The way you're supposed to do this in YUI appears to be something like:

YAHOO.util.History.navigate('p1','1');
YAHOO.util.History.navigate('p2','2');
YAHOO.util.History.navigate('p3','3');

If you want the browser URL string to be http://somesite.com/somesubdomain#p1=1&p2=2&p3=3

The Calendar Example in the docs demonstrates this.

Dmitry Pashkevich
  • 13,431
  • 9
  • 55
  • 73
  • Calender example uses multiple module, which I do not want to do. So, it seems its not possible to tell browser what part of my string should be encoded. But, anyway thanks for the answer. I will either close the question or accept the answer. – hrishikeshp19 Jun 04 '12 at 19:28
  • I don't see a problem here, just let YUI to urlencode the state data if you want to assign it to a single module and then just decode and parse it in your module's logic. YUI needs to encode that data in order to be able to tell between different modules. If you need suggestions on improving your application architecture, open a new question. – Dmitry Pashkevich Jun 04 '12 at 19:35