HTML labels are displayed with ascii characters? - javascript

I have html file and it has some labels.when i usually get the values through request parameters appended to URL. when i display the values in an html they will be dispalyed with ASCII chars as below. am getting values with %20 from my URL itself. And am reading them in servlet and forward to some.jsp
Name=Hellow%20how%20are%20you
But i want out put as:
Name=Hello how are you
Do i need to do anything in my html to render the value without ASCII ?
Thanks!

Use unescape():
Name = unescape("Hellow%20how%20are%20you"); // = "Hellow how are you"
Example fiddle

I see that you have servlet here , so you talk about java not javascript i guess.
Any way In java you got StringEscapeUtils from Commons Lang to escape/unescape.
In Javascript you escape through encodeURIComponent, but I think the Commons component I gave to you will satisfy your needs.

I solved it by putting :
URLDecoder.decode(request.getQueryString(), "UTF-8");

Related

How to pass a "?" as a routing parameter in express while using node js

I am creating a blog website for my college using express and nodejs this might be a silly question but I need an answer.
as you can see from the following piece of code-
enter code here
cons express = require("express");
.....
.....
app.get("/blog/:title_of_blog", function(request, response){
var title_requested = request.params.title_of_blog;
console.log(title_requested);
})
It works fine in all the cases I tried except when if a user enters a string in as a routing parameter like "what is your name?" then it console logs only "what is your name"
So a question mark gets excluded here on which further process depends as it should be exactly the same,
Is there any way I could fix this???
If you do need any additional information please do let me know
Because express will understand ? is for starting a query string. So usually if you want to put blog title to URL, you can parse title to slug using some lib.
In JavaScript, PHP, and ASP there are functions that can be used to URL encode a string.
PHP has the rawurlencode() function, and ASP has the Server.URLEncode() function.
In JavaScript you can use the encodeURIComponent() function.
source
When you apply encoding on your blog title and pass URL Become like this
/blog/what%20is%20your%20name%20%3F
You will got the exact result as you want. :)
certain Characters such as ?, # are not allowed in URL Parsing or I recently discovered even while storing cookies, but there is a simple way to avoid these problems and get your string in the desired way
var encodedString = encodeURI(rawString);
this above line turns all the characters such as # into an easily parsable string and after the string is processed whenever you need the actual string you just have to type one line to get the original string
var originalString = decodeURI(enodedString);

Regular expression in javascript

I have two strings
http://dfdkdlkdkldkldkldl.jpg (it is image src which is staring with http and ending with image)
http://fflffllkfl
now i want to replace http:// with sometext only on those url which having image
what i tried (http(s?):)|([/|.|\w|\s])*.(:jpg|gif|png)
it replacing http of both string.
Any body can help
Thanks
Here is a valid regex:
(https?:)\/\/.*(jpg|gif|png)
That will only match the "image" url. You can play around with it online here: http://regex101.com/
Edit
Basically, your Regex was not only invalid, but too convoluted. You had a sub-group for the "s" on "https", which wasn't needed according to the problem you proposed. Also, you had the OR operand trying to separate the http part and the rest of the url, which made no sense..., lastly, you were grouping the text between ":" and the dot ".", which again, according to your problem description it wasn't needed.
Hope that helps
Edit 2
Ok, so I don't know how exactly the replacement is being done, you're not using your code, you're using a page for that, but here is how you should be doing it:
"http://dfdkdlkdkldkldkldl.jpg".replace(/(https?:)(\/\/.*)(jpg|gif|png)/, "lalala$2$3")
Note that the RegEx changed to: (https?:)(\/\/.*)(jpg|gif|png)
If you try it with the other url: "http://fflffllkfl".replace(/(https?:)(\/\/.*)(jpg|gif|png)/, "lala$2$3") it won't replace anything.
Try this:
myString.replace(/(https?:\/\/)(.*\.jpg|gif|png)/, "some string $2");

How to encode and decode HTML special characters to get entity name

The character set is mentioned at Special Character Map. I need a Java-script or J-Query encoding code to get entity name.
for e.g. if I pass £ then I should get "&pound ;" or for ¥ it should return "&yen ;".
Even I copy the symbols instead of typing in then also it should work.
I am trying to use following J-Query code but it doesn't seem to work when I copy-paste strings.
function krEncodeEntities() {
var s = $('#input').val();
return $('#lblEncode').text($("<div/>").text(s).html());
}
function krDencodeEntities() {
var s = $('#lblEncode').text();
return $('#lblDecode').text($("<div/>").html(s).text());
}
Can anyone please help me?
JavaScript has no concept of HTML identities. To JS, everything is UCS16 (a forerunner of UTF16).
You have a couple of options.
Option 1
Make a big translation object of characters and their identities.
Option 2
See if some other form of encoding will work for you.
When are you supposed to use escape instead of encodeURI / encodeURIComponent?

How do I insert HTML into Mongodb?

I keep getting kicked out of the shell when I try to paste in an HTML text file. How can this be done? Do I first need to use some javascript to encode it or something?
Example:
db.test.insert({x:"<div id='product-description'><p>Who hasn’t wished for a mini-Roomba to handle the arduous task of cleaning their iPhone screen? Now your dreams have come true!</p> <p>See the Takara web page for a <a href='http://www.takaratomy.co.jp/products/automee/' title='automee s' target='_blank'>demo video.</a></p>
<p><strong>Colors: </strong> White, Red, Orange and Blue<br>
Runs on a single AA battery.</p>
<p>1,575 yen</p><!-- end #product-description --></div>"})
EDIT
I put only single quotes inside my html and wrapped the whole thing in double-quotes, but still no good. shell error:
> j = ({z:"<div id='product-description'><p>Who hasn
---
Unicode text could not be correctly displayed.
Please change your console font to a Unicode font (e.g. Lucida Console).
---
’
bye
You need to remove or encode the control characters in your string.
For example, paste your text in here, and encode to UTF-8 ECMAScript (which means javascript strings).
ps: here an article about strings in javascript. tells you what needs escaping. http://docstore.mik.ua/orelly/webprog/jscript/ch03_02.htm
rompetroll provided some good references to get started. I too found a good solution online which works fine:
Link
But I think there is still some confusion regarding "displaying html back to browser from db". So, following are the steps which removes this confusion :
Step 1: Store html in mongodb
Mongo store everything in form of object. Basically convert your html into utf-8 or ASCII complied string and then store it to mongo.
<div>hi!</div> [from]
<div>hi!</div> [to UTF-8]
Step 2: Display html to browser
This is the step which is the source of confusion to some. People often forget to convert string back to html before displaying it to browser.
You have to decode your html using the same method you used for encoding.
<div>hi!</div> [output from mongo (decode it to html)]
<div>hi!</div> [browser output of above string]
(browser will read it as string and display as it is and not html(hi!) and hence needs to be decoded)
Link that I have provided have reference for both the steps.
Hope this will help
Cheers :)
Use compass, you don't need to encode. Studio3T editor also works, but you need a license.
To add html through MongoDB console, you need to escape your string.
Use this: https://www.freeformatter.com/json-escape.html#ad-output
Steps
copy HTML
go to the link above, paste HTML click on "Escape"
copy the Escaped HTML
Insert HTML (e.g: db.Emails.updateMany({name: 'email/seller'}, {$set: {template: "<!DOCTYPE html\">\r\n<html lang=\"en\"> ..."}})

javascript escape problem with unicode characters

I use the following jquery code to load some date on a specific event from external file:
$("#container").load("/include/data.php?name=" + escape(name));
if the javascript "name" variable contains unicode characters it sends some encoded symbols to data.php file, something like this: %u10E1
How can I deal with this encoded symbols? I need to convert them back to readable one.
When I remove the escape function and leave just "name" variable the code doesn't work any more...
Can anyone please help?
If you want to do this manually, then you should be using encodeURIComponent, not escape (which is deprecated)
The jQuery way, however, would be:
$("#container").load("/include/data.php", { "name": name });
Either way PHP should decode it automatically when it populates $_GET.
This may help you.
javascript - how to convert unicode string to ascii

Categories

Resources