CaptainCarl
CaptainCarl

Reputation: 3489

Encoding javascript url to pass #(hashtag)

I'm passing a URL to a PHP file in which to change some colors etc. One of the colors is #ccc and is passed up as &background=#ccc which breaks my PHP file (It stops my $_GET parameters at that hashtag, everything after that is not passed along)

I've tried encoding my url as encodeURIComponent(url) which also encodes my ? and & I use for my $_GET string. When using encodeURI() the hashtag isn't encoded at all.

Any ideas how to pass all my vars to my PHP file so I can read them there?

Please note that it's a stylesheet href. I can't/won't use jQuery's $.get() method but just parse a tag and append it in my as such:

<link href="http://localhost:8888/test.php/layout.scss?primarycolor=hotpink&secondarycolor=white&background=#ccc&textcolor=black&linkcolor=black&font=Futura&" type="text/css" rel="stylesheet" id="custom-style">

Upvotes: 2

Views: 5350

Answers (3)

Marius
Marius

Reputation: 4016

Hash tags are not passed to the server, so that's why they're breaking your script. As for encoding, if encodeURIComponent() works, you'll probably have to encode each value in the URL. Alternatively, you can just replace the # symbol in your url with %23 using a simple string replace.

Upvotes: 1

EM-Creations
EM-Creations

Reputation: 4301

You could implement a proprietary encoding system. For example, saying that "#" is equal to "!-!" for example.

Then in your server-side PHP code you can run a str_replace("!-!", "#", $getVar) statement to use the colour code correctly. Not ideal however.

Also, try: htmlentities and html_entity_decode: http://php.net/htmlentities

Upvotes: 0

Amadan
Amadan

Reputation: 198324

encodeURIComponent is correct, but you only want to use it on the #ccc bit. If you are doing it by hand (as in, hardcoding in the <link> tag), then use %23 instead of #.

Upvotes: 5

Related Questions