escape string (% character) in encodeURI in JavaScript - javascript

I have a problem where I need to preserve %2F in a URI string. However, encodeURI encodes % as %25 (as normal), so the whole string ends up being %252F instead of %2F. How can I escape this so the % doesn't get encoded. This is happening deep within a framework, so JS manipulation is not an option, it needs to be done via string escaping. I've tried using \ in a number of ways, none of it is working.

If you have control during the decode process, an unescape() before decodeURI would help.
This way, every double encoding is consistently handled.
For example:
decodeURIComponent(unescape("http%253A%252F%252Fabcd.com"));
will return
'http://abcd.com'

Related

JavaScript not decoding parameter

So from the textarea I take the shortcode %91samurai id="19"%93 it should be [samurai id="19"]:
var not_decoded_content = jQuery('[data-module_type="et_pb_text_forms_00132547"]')
.find('#et_pb_et_pb_text_form_content').html();
But when I try to decode the %91 and %93
self.content = decodeURI(not_decoded_content);
I get the error:
Uncaught URIError: URI malformed
How can i solve this problem?
The encodings are invalid. If you can't fix the whatever-system-produces-them to correctly produce %5B and %5D, then your only option is to do a replacement yourself: replace all %91 with character 91 which is '[', then replace all %93 with character 93 which is ']'.
Note that javascript String Replace as-is won't do "Replace all occurrences". If you need that, then create a loop (while it contains(...) do a replace), or search the internet for javascript replace all, you should find plenty results.
And a final note, I am used to using decodeURIComponent(...). If you can make the whatever-system-produces-them to correctly produce %5B and %5D, and you still get that error, then try using decodeURIComponent(...) instead of decodeURI(...).
The string you're trying to decode is not a URI. Use decodeURIComponent() instead.
UPDATE
Hmm, that's not actually the issue, the issues are the %91 and %93.
encodeURI('[]')
gives %5b%5d, it looks like whatever has encoded this string has used the decimal rather than hexadecimal value.
Decimal 91 = hex 5b
Decimal 93 = hex 5d
Trying again with the hex values
decodeURI('%5bsamurai id="19"%5d') == '[samurai id="19"]'
I know this is not the solution you want to see, but can you try using "%E2%80%98" for %91 and "%E2%80%9C" for %93 ?
The %91 and %93 are part of control characters which html does not like to decode (for reasons beyond me). Simply put, they're not your ordinary ASCII characters for HTML to play around with.

Detect whether JavasScript string has been encoded using encodeURIComponent

I'm working to integrate some code with a third party, and sometimes a string argument they pass to a Javascript function I'm writing will be encoded using encodeURIComponent, sometimes it won't be.
Is there a definitive way to check whether it's been encoded using encodeURIComponent
If not, I'll do the encoding then
You could decode it and see if the string is still the same
decodeURIComponent(string) === string
Not reliably, especially in the case where a string may be encoded twice:
encodeURIComponent('http://stackoverflow.com/')
// yields 'http%3A%2F%2Fstackoverflow.com%2F'
encodeURIComponent(encodeURIComponent('http://stackoverflow.com/'))
// yields 'http%253A%252F%252Fstackoverflow.com%252F'
In essence, if you were to try and detect the string encoding when the passed argument is not actually encoded but has qualities of an encoded string, you'd be decoding something you shouldn't.
I'd recommend adding a second parameter in the definition "isURIComponent".
However, if you wanted to attempt, perhaps the following would do the trick:
if ( str.match(/[_\.!~*'()-]/) && str.match(/%[0-9a-f]{2}/i) ) {
// probably encoded with encodeURIComponent
}
This tests that the non alphanumeric characters that don't get encoded are intact, and that hexadecimals exist (e.g. %20 for a space)

Pass a parameter containing '%' in URL?

While passing my url, for example something:8000/something.jsp?param1=update&param2=1000&param3=SearchString%&param4=3 , I am getting following error:
Bad Request
Your browser sent a request that this server could not understand.
I know SearchString% which I need to pass as a parameter, has the issue. Then how to pass a
parameter containing '%' in URL??
Use %25 in place of %
In URLs % has a special meaning as an escape character
Special characters like (space) can be encoded like %20 (the ascii code for space/32 in hex)
Therefore a percent sign itself must be encoded using the hex code for % which happens to be 25
You can use http://www.asciitable.com/ to look up the appropriate hex code under the hx column
Alternatively, if you are doing this programatically (ie. with javascript) you can use the builtin function escape() like escape('%')
See this: Encode URL in JavaScript?
Basically you need to make sure the variables you are passing are encoded (the '%' character is a special character in URL encoding).
Any special characters - %,?,&, etc... need to be encoded. They are encoded with '%' and their hex number. So '%' should become '%25', '&' becomes '%26', etc.
Update: see When are you supposed to use escape instead of encodeURI / encodeURIComponent? for why you should avoid using escape.

Converting backslashes into forward slashes using javascript does not work properly?

I have a javascript variable comming from legacy system with backslashes into forward slashes:
'/46\465531_Thumbnail.jpg'
and I am trying to convert into this:
'/46/465531_Thumbnail.jpg'.
There is no way to fix the problem on the legacy system.
Here is the command I am running on IE8 browser:
javascript:alert("/46\465531_Thumbnail.jpg".replace(/\\/g,"/"));
as response I get:
---------------------------
Message from webpage
---------------------------
/46&5531_Thumbnail.jpg
---------------------------
OK
---------------------------
actually I just want to be translated as '/46/465531_Thumbnail.jpg'
What is wrong?
You need to double the backslash in your string constant:
alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
If your legacy system is actually creating JavaScript string constants on your pages with embedded, un-quoted (that is, not doubled) backslashes like that, then it's broken and you'll have problems. However, if you're getting the strings via some sort of ajax call in XML or JSON or whatever, then your code looks OK.
It is actually interpreting \46 as an escape-code sequence for the character &. If you are going to hard-code the string, you need to escape the \:
alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
^^ change \ to \\
Sample: http://jsfiddle.net/6QWE9/
The replacement part isn't the problem, it's the string itself. Your string:
"/46\465531_Thumbnail.jpg"
isn't /46\465531. Rather, the backslash is acting as an escape character. You need to change it to:
javascript:alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
ie, escapeing the backslash with a backslash.
Nothing wrong with the replace. The input is wrong.
javascript:alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
^
\---------------- need to escape this!

Some encoded Javascript that I need in plain text

I'm having some issues trying to decode some javascript.. I have no idea what kind of encoding this is.. i tried base 64 decoders etc. If you can please help me out with this, here's a fragment of the code:
\x69\x6E\x6E\x65\x72\x48\x54\x4D\x4C","\x61\x70\x70\x34\x39\x34\x39\x3
Any ways I can get plain text from that?
Thanks!
\xNN is an escape sequence. NN is a hexidecimal number (00 to FF) that represents a Latin-1 character.
Escape sequences are interpreted literally within a string. So:
"\x69" === "i" // true
The escape() function encodes a
string.
This function makes a string portable,
so it can be transmitted across any
network to any computer that supports
ASCII characters.
This function encodes special
characters, with the exception of: * #
- _ + . /
The reverse of escape() is the unescape() function.
Try this:
alert(unescape("\x69\x6E\x6E\x65\x72\x48\x54\x4D\x4C\x61\x70\x70\x34\x39\x34\x39\x3"));
Edit: As J-P mentioned unescape isn't really needed here after all.
These are simply hex-values of symbols.
\x69 = i, etc. First several letters: "innerHTML", "ap…"
I think you should use window.unescape(), or unescape()

Categories