Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
JavaScript URL Decode function

I have an already encoded URL string printed in my HTML template via Django. When I place this in a call to location.replace() it gets mangled by some JavaScript that mangles the = and % already present in the query string, resulting in the subsequent URL (out of my domain) not knowing what to do with it.

How do I prevent JavaScript from changing it?

EDIT: example url string:

'http://destination.com/?name=https%3A%2F%2Fexample.com%2F&nextparam=nextvalue'

passing above into location.replace() results a redirect to:

http://destination.com/?name%3Dhttps%253A%252F%252Fexample.com%252Fnextparam=nextvalue

which is obviously incorrect.

The URL has as one of it's query string parameters a URL. The safe encoded characters passed from Django are from the set of characters in the string ':/', basically so the 'http://example.com/' gets encoded correctly. Fine. '=%&' are all untouched parts of the query string.

In my encoded string that works outside of js (eg in anchor tag href) this links to the correct url.

But when I put it in window.location when it redirects it escapes all characters in the query string and removes '&' for some reason - even the '%' used to encode the original URL parameter in the qs. Checking source shows the string is identical to the one in the a tag above.

Is there anyway to prevent javascript location attribute escaping stuff prior to the redirect?

share|improve this question

marked as duplicate by jbabey, Eitan T, Ja͢ck, Bergi, Jason Sturges Sep 26 '12 at 14:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
If you look at the revision history, you can see that the link to the duplicate was inserted while you were editing, and thus was removed when you submitted your edit. Bad timing. –  Daniel Fischer Feb 1 '13 at 17:27

2 Answers 2

You should decode the query string before calling location.replace() with it.

JavaScript doesn't have a built in method for encoding/decoding strings, but there is a library called php.js that can help you. See this link for a function for decoding urls. This library is widely supported.

share|improve this answer
    
is there a built-in method for decoding query strings? –  user1561108 Sep 26 '12 at 13:09
    
@user1561108 See my edit –  Samuel Sep 26 '12 at 13:13
    
I've expanded on my problem. I only encode some characters prior; others should be left untouched. Sending the url to window.location or location.replace() etc encodes all of the characters prior to redirect for some reason. –  user1561108 Sep 26 '12 at 14:14

Consider decoding the query string before calling location.replace() with it.

You can do this using the built-in decodeURIComponent function.

share|improve this answer
    
Please see my edit –  user1561108 Sep 26 '12 at 14:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.