This question already has answers here:
How do I correctly insert unicode in an HTML title using JavaScript?
(2 answers)
Closed 7 years ago.
help me pls.
In Unicode symbol & = "&" + "#38;" If I add it to HTML like this
<div id="div" title="&"></div>
ALL OK! I create symbol in title, like this
div id="div" title="&"></div>
But, when I add Unicode in HTML via JavaScript:
var div = document.getElementById('div');
div.setAttribute('title', "&");
I have a bad result:
<div id="div" title="&"></div>
How I can add Unicode in html attribute via JavaScript and get the correct result like it:
<div id="div" title="&"></div>
Thanks for help!
AND
IF I do it:
var code = 26;
div.setAttribute('title', "\x" + code);
I have a error. How I can fix it?
HTML entity escaping (e.g. &) is, as the name implies, only necessary in HTML. It's not necessary in Javascript; you can set the character literally:
div.setAttribute("title", "&");
If you need to escape a character, you can do so using a hexadecimal character escape:
div.setAttribute("title", "\x26");
or a Unicode character escape:
div.setAttribute("title", "\u0026");
For the & character in Javascript, you do not have to escape it. Otherwise, you should can escape Unicode in Javascript with \u, but most of the time you will not have to.
Related
This question already has answers here:
angularjs to output plain text instead of html
(8 answers)
Closed 5 years ago.
I have a very simple requirement. I have two strings as follows :
htmlStr = "<strong>News Body</strong>";
htmlStr1 = "<strong>News Body2</strong>";
I use innerHTML of div tag to display these strings on a html page
<div [innerHTML]="htmlStr"></div>
<div [innerHTML]="htmlStr1"></div>
Output I get is
My expected output is :
How do i remove html encoding from htmlStr?
The output is correct. You should replace the special characters with the brackets by e.g.
strHtml = strHtml.split(‘<’).join(‘<‘).split(‘>’).join(‘>’)
Try pipe for dynamic encoding html string
stackblitz demo - it's working correct do like this
This question already has answers here:
Remove ALL white spaces from text
(14 answers)
Closed 6 years ago.
Can some one help me to create a regular expression in Javascript to remove a whitespace from href link and replace whitespace to hyphen to in my content?
For example:
<a class="card" href=http://www.eee.com/sffsd/sdfs/Aks's Reb outsider/4234234234324>
it should convert it into
<a class="card" href=http://www.eee.com/sffsd/sdfs/Aks's-Reb-outsider/4234234234324>
A couple of things, like I said in the comment, replacing the spaces with dashes should be as easy as:
link.href = link.href.replace(/ /g, '-');
//or in php:
$href = preg_replace('/ /', '-', $href);
(JS only): using the g flag ensures the entire string is searched for spaces, and replaces them all with dashes.
The better question to ask is: how did the spaces get there in the first place?
My first port of call would be to look at the code generating the markup, and fix the problem there. You shouldn't be writing code fixing the output of code that is broken. Fix the bug, don't accommodate it.
Arguably, the URL's should be properly escaped, rather tan using regex, the URI should've been passed through a function like encodeURI to convert all spaces to %20 etc...
use this regex format
link.href = link.href.replace(/\s/g, '-');
This question already has answers here:
How to convert characters to HTML entities using plain JavaScript
(12 answers)
Closed 8 years ago.
I have a string that contains text including special characters like apostrophes, quotes, ampersand etc. that I would like to convert to HTML character codes, e.g. to show ' instead of an apostrophe etc. (as per the following list: http://www.w3.org/MarkUp/html-spec/html-spec_13.html.
I do not necessarily need to convert letters and spaces as long as the above and some other basic characters are encoded correctly.
For a temporary workaround I use simple replaces like str = str.replace("'", "'") but would like to avoid this as it only covers certain characters.
I was thinking of encodeURI or encodeURIcomponent but would like to know what is the best and easiest way to achieve this without encoding more than necessary.
Can someone tell what is the best way here if the idea is to avoid any issues with other JS where this is inserted dynamically, esp. thinking of single quotes, double quotes and any other character that could cause issues here (referring to JavaScript / jQuery and English language only) ?
function encodeEntities(value) {
return $('<div />').text(value).html();
}
This question already has answers here:
Nesting quotes in JavaScript/HTML
(4 answers)
Closed 8 years ago.
I have a piece of javascript that creates an HTML table, saving each row of the table into an array. I want one of the table cells on each row to be clickable.
The following code works fine and when cell3 is clicked, it displays a message box:
array.push("<tr><td>cell1</td><td>cell2</td><td onclick='alert(this);'>cell3</td></tr>")
My problem is that if I wanted the message box to say "hello", I can't get it to work because of the quotes, any ideas?
It's a quote within a quote within a quote and my brain is melting.
Thanks for any help in advance.
try putting a backslash \ before the quotes around the word hello (for example).
you can programmatically do that if it works out for you when hardcoded... but let's see if it helps you first.
the following answer also suggests how to programmatically replace all quotes in your string with escaped quotes, so you don't have to hard code them:
How do I replace a double-quote with an escape-char double-quote in a string using javascript?
sample:
str.replace(/"/g, '\\"');
You can escape the " char using the \ before, for sample:
array.push("<tr><td>cell1</td><td>cell2</td><td onclick=\"alert(this);\">cell3</td></tr>");
Does this work?
var alertMessage = "HELLO!";
array.push("<tr><td>cell1</td><td>cell2</td>
<td onclick='alert(" + alertMessage +");'>cell3</td></tr>")
This question already has answers here:
How to convert unicode in JavaScript?
(4 answers)
Closed 9 years ago.
I am getting data returned in a JSON file with Unicode characters. How to replace Unicode characters as per my example?
\u003cli class=\"channels-content-item\"\u003e\n
\n
\u003cdiv class=\"shmoovie-content-cell\"\u003e\n
\u003ca href=\"\/movie\/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e
After replacing with regex:
\u003c must be replaced by <
\u003e must be replaced by >
\/ must be replaced by /
\" must be replaced by "
How to do that?
Using the bit of the string you posted I put this fiddle together that shows how to just use the string value you have (like in this SO answer I mentioned in the comments).
HTML
<div id="content"></div>
JS
var s = "\u003cli class=\"channels-content-item\"\u003e\n\n\u003cdiv class=\"shmoovie-content-cell\"\u003e\n\u003ca href=\"\/movie\/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e";
var div = document.getElementById('content');
div.innerHTML = s;
console.log(s);
Which sets the HTML content for the div with the elements:
<li class="channels-content-item">
<div class="shmoovie-content-cell">
<a href="/movie/the-makeover" class="ux-thumb-wrap contains-addto yt-uix-sessionlink" data-sessionlink="ei=Oo21UdLqM8aDhgHc_IHYCA">
Although it's not valid HTML, javascript seems to figure it out, at least it does in Chrome.
Not sure that regex is the best idea...The below solves the problem using the replace function built into JavaScript.
DEMO: http://jsfiddle.net/abc123/5DFUb/1/
var str = "\u003cli class=\"channels-content-item\"\u003e\n \n \u003cdiv class=\"shmoovie-content-cell\"\u003e\n \u003ca href=\"/movie/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e";
var newStr = str.replace("\\u003c", "<").replace("\\u003e",">").replace("\\/","/").replace("\\\"","\"");
alert(newStr);