change xef/xbc/x9e and other to normal character html - javascript

i get the data from a server and that is transferred to the main page with this command
document.getElementById("ft").innerHTML +=(endata);
the problem is that the server send data with those character and many more
\xe2\x80\xa6
\xef\xbd\x9e
or some Chinese, Korean, and Japanese characters also.
put having this \xef\xbd\x9e character instead of > is not beautiful
so how could I do to make the insert code send by the server not have this code?

Seems that they send uri encoded character.
Try this:
const encoded = '\xe2\x80\xa6';
console.log(decodeURI(encoded));

Related

Javascript encodeURIComponent and Java decode issue

I have a certain text I am encoding in JS using encodeURIComponent. The original text is
weoowuyteeeee !_.
Test could you please resubmit again?
I am doing the following in my JS code before sending it.
var text = encodeURIComponent($("#txt11").val());
Should I not be doing that?
Once I encode it using encodeURIComponent, it becomes
weoowuyteeeee%2520!_.%252C%250A%250ATest%252C%2520%2520could%2520you%2520please%2520resubmit%2520again%253F
I'm trying to decrypt the same on the Java side using
String decodedString1 = URLDecoder.decode(myObject.getText(), "UTF-8");
but I see this as the output, not the original text. What am I doing wrong?
weoowuyteeeee%20!_.%2C%0A%0ATest%2C%20%20could%20you%20please%20resubmit%20again%3F
You are encoding your data twice.
Initially, you have encoded your data and later it is encoded again.
Eg: Let your text be
Hello World
After encoding it becomes
Hello%20World
If you encode again it becomes
Hello%2520World
Reason
% from %20 is encoded to %25. So the space becomes %2520.
Normal AJAX can will automatically encode your data before sending to the server side. Check where the 2nd encoding is happening.

How to encode and decode all special characters in javascript or jquery?

I want to be able to encode and decode all the following characters using javascript or jquery...
~!##$%^&*()_+|}{:"?><,./';[]\=-`
I tried to encode them using this...
var cT = encodeURI(oM); // oM holds the special characters
cT = cT.replace(/[!"#$%&'()*+,.\/:;<=>?#[\\\]^`{|}~]/g, "\\\\$&");
Which does encode them, or escape them rather, but then I am trying to do the reverse with this...
decodeURIComponent(data.convo.replace(/\+/g, ' '));
But, it's not coming out in any way desired.
I've built a chat plugin for jquery, but the script crashes if someone enters a special character. I want the special characters to get encoded, then when they get pulled out of the data base, they should be decoded. I tried using urldecode in PHP before the data is returned to the ajax request but it's coming out horribly wrong.
I would think that there exists some function to encode and decode all special characters.
Oh, one caveat for this is that I'm wrapping each message with html elements, so I think the decoding needs to be done server side, before the message is wrapped, or be able to know when to ignore valid html tags and decode the other characters that are just what the user wanted to type.
Am I encoding/escaping them wrong to begin with?
Is that why the results are horrible?
This is pretty simple in javascript
//Note that i have escaped the " in the string - this means it still gets processed
var exampleInput = "Hello there h4x0r ~!##$%^&*()_+|}{:\"?><,./';[]\=-`";
var encodedInput = encodeURI(exampleInput);
var decodedInput = decodeURI(encodedInput);
console.log(exampleInput);
console.log(encodedInput);
console.log(decodedInput);
Just encode and decode the input. If something else is breaking in your script it means you are not stripping away things that you are somehow processing. It's hard to provide an accurate answer as you can see encoding and decoding the URI standards does not crash things. Only the processing of this content improperly would cause issues.
When you output the content in HTML you should be encoding the HTML entities.
Reference this thread Encode html entities in javascript if you need to actually encode for display inside HTML safely.
An additional reference on how html entities work can be found here: W3 Schools - HTML Entities and W3 Schools - HTML Symbols

Javascript convert string from utf-8 to iso-8859-1

I know this sounds bad, but it's necessary.
I have a HTML form on a site with utf-8 charset which is sent to a server which works with the iso-8859-1 charset. The problem is that the server doesn't understand correctly characters we use in Spain like à, á, è, é, ì, í, ò, ó, ù, ú, ñ, ç and so on. So if I search something like artículo it answers nothing found with artículo.
I send the form with ajaxform (http://malsup.com/jquery/form/), and the code loks like this:
$(".form-wrap, #pagina").on("submit", "form", function(event){
event.preventDefault();
$(this).ajaxSubmit({
success: function(data){
$("#temp").html(data);
//Handle data in #temp div
$("#temp").html('');
}
});
return false;
});
My problem is: I have no acces to the search server and I cannot change the whole website to iso-8859-1 encodig since this would break other stuff.
I have alredy tried with no succes these scripts:
http://phpjs.org/functions/utf8_decode/
http://phpjs.org/functions/utf8_encode/
http://ecmanaut.blogspot.com.es/2006/07/encoding-decoding-utf8-in-javascript.html
May I be doing everything wrong?
Edit: the escape function isn't useful for me as it turns these spacial chars into % prefixed codes which are useless to the server, it then searches for art%EDculo.
Edit using the encodeURIComponent function the server understands art%C3%ADculo. P.S. I just use the word artículo for testing but the solution it should cover all special chars.
I have a HTML form on a site with utf-8 charset which is sent to a server which works with the iso-8859-1 charset.
You can try setting form accept-charset:
<form accept-charset="iso-8859-1">
....
</form>
Finally the one who runs that server gave us access to the server and we made an utf-8 compatible php script. So I don't care any more.

Converting textarea value into a valid JSON string

I am trying to make sure input from user is converted into a valid JSON string before submitted to server.
What I mean by 'Converting' is escaping characters such as '\n' and '"'.
Btw, I am taking user input from HTML textarea.
Converting user input to a valid JSON string is very important for me as it will be posted to the server and sent back to client in JSON format. (Invalid JSON string will make whole response invalid)
If User entered
Hello New World,
My Name is "Wonderful".
in HTML <textarea>,
var content = $("textarea").val();
content will contain new-line character and double quotes character.
It's not a problem for server and database to handle and store data.
My problem occurs when the server sends back the data posted by clients to them in JSON format as they were posted.
Let me clarify it further by giving some example of my server's response.
It's a JSON response and looks like this
{ "code": 0, "id": 1, "content": "USER_POSTED_CONTENT" }
If USER_POSTED_CONTENT contains new-line character '\n', double quotes or any characters that are must be escaped but not escaped, then it is no longer a valid JSON string and client's JavaScript engine cannot parse data.
So I am trying to make sure client is submitting valid JSON string.
This is what I came up with after doing some researches.
String.prototype.escapeForJson = function() {
return this
.replace(/\b/g, "")
.replace(/\f/g, "")
.replace(/\\/g, "\\")
.replace(/\"/g, "\\\"")
.replace(/\t/g, "\\t")
.replace(/\r/g, "\\r")
.replace(/\n/g, "\\n")
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029");
};
I use this function to escape all the characters that need to be escaped in order to create a valid JSON string.
var content = txt.val().escapeForJson();
$.ajax(
...
data:{ "content": content }
...
);
But then... it seems like str = JSON.stringify(str); does the same job!
However, after reading what JSON.stringify is really for, I am just confused. It says JSON.stringify is to convert JSON Object into string.
I am not really converting JSON Object to string.
So my question is...
Is it totally ok to use JSON.stringify to convert user input to valid JSON string object??
UPDATES:
JSON.stringify(content) worked good but it added double quotes in the beginning and in the end. And I had to manually remove it for my needs.
Yep, it is totally ok.
You do not need to re-invent what does exist already, and your code will be more useable for another developer.
EDIT:
You might want to use object instead a simple string because you would like to send some other information.
For example, you might want to send the content of another input which will be developed later.
You should not use stringify is the target browser is IE7 or lesser without adding json2.js.
I don't think JSON.stringify does what you need. Check the out the behavior when handling some of your cases:
JSON.stringify('\n\rhello\n')
*desired : "\\n\\rhello\\n"
*actual : "\n\rhello\n"
JSON.stringify('\b\rhello\n')
*desired : "\\rhello\\n"
*actual : "\b\rhello\n"
JSON.stringify('\b\f\b\f\b\f')
*desired : ""
*actual : ""\b\f\b\f\b\f""
The stringify function returns a valid JSON string. A valid JSON string does not require these characters to be escaped.
The question is... Do you just need valid JSON strings? Or do you need valid JSON strings AND escaped characters? If the former: use stringify, if the latter: use stringify, and then use your function on top of it.
Highly relevant: How to escape a JSON string containing newline characters using javascript?
Complexity. I don't know what say.
Take the urlencode function from your function list and kick it around a bit.
<?php
$textdata = $_POST['textdata'];
///// Try without this one line and json encoding tanks
$textdata = urlencode($textdata);
/******* textarea data slides into JSON string because JSON is designed to hold urlencoded strings ******/
$json_string = json_encode($textdata);
//////////// decode just for kicks and used decoded for the form
$mydata = json_decode($json_string, "true");
/// url decode
$mydata = urldecode($mydata['textdata']);
?>
<html>
<form action="" method="post">
<textarea name="textdata"><?php echo $mydata; ?></textarea>
<input type="submit">
</html>
Same thing can be done in Javascript to store textarea data in local storage. Again textarea will fail unless all the unix formatting is deal with. The answer is take urldecode/urlencode and kick it around.
I believe that urlencode on the server side will be a C wrapped function that iterates the char array once verses running a snippet of interpreted code.
The text area returned will be exactly what was entered with zero chance of upsetting a wyswyg editor or basic HTML5 textarea which could use a combination of HTML/CSS, DOS, Apple and Unix depending on what text is cut/pasted.
The down votes are hilarious and show an obvious lack of knowledge. You only need to ask yourself, if this data were file contents or some other array of lines, how would you pass this data in a URL? JSON.stringify is okay but url encoding works best in a client/server ajax.

How to send Korean characters in URL?

I want to send some Korean values from page1.html on page sumbit to page2.html. But the Korean fonts are getting encoded. Can any one help me with it. I have this meta tag in both the screens.
window.location.href = "page2.html?value='풍경' these Korean character are getting encoded in few mobile devices.
for this page the values is encode as
page2.html?value= %EC%82%EB%AC%BC
Korean characters (and any other non-URL-safe characters) are %-encoded for the transaction. However, when received by the server and put into (for instance) PHP's $_GET array, they are decoded automatically so you don't have to worry about it.
I'm still not completely clear on what you actually asking, but if you correctly construct Url it should be much easier to reason on what should/should not be happening:
// to construct correctly encoded Url:
var encodedValue = encodeURIComponent("'풍경'");
window.location.href = "page2.html?value=" + encodedValue;
// to decode back from query parameter (if needed)
var decoded = decodeURIComponent(encodedValue);
Check out Encode URL in JavaScript? for guidance on encoding Urls with JavaScript.

Categories