Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

removed unnecessary (and possibly harmful) use of setAttribute; added 20 characters in body
Source Link
Marcel Korpel
  • 21.9k
  • 6
  • 63
  • 80
<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>setAttribute<title>Decode test<HTML entities using JavaScript</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 function html_entity_decode(str) {
 var p = document.createElement("p");
 p.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
 return p.innerHTML;
 }
 var theValue = html_entity_decode('A &rarr; B');
 document.getElementById('formula').setAttribute('title',title theValue);= theValue;
 </script>
 </body>
</html>

Credits to The JavaScript Source.

EDIT: I changed the original code to only use innerHTML on a <p> element, instead of innerHTML and value of a <textarea>.

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>setAttribute test</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 function html_entity_decode(str) {
 var p = document.createElement("p");
 p.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
 return p.innerHTML;
 }
 var theValue = html_entity_decode('A &rarr; B');
 document.getElementById('formula').setAttribute('title', theValue);
 </script>
 </body>
</html>

Credits to The JavaScript Source.

EDIT: I changed the original code to only use innerHTML on a <p> element, instead of innerHTML and value of a <textarea>.

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>Decode HTML entities using JavaScript</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 function html_entity_decode(str) {
 var p = document.createElement("p");
 p.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
 return p.innerHTML;
 }
 var theValue = html_entity_decode('A &rarr; B');
 document.getElementById('formula').title = theValue;
 </script>
 </body>
</html>

Credits to The JavaScript Source.

EDIT: I changed the original code to only use innerHTML on a <p> element, instead of innerHTML and value of a <textarea>.

Edited html_entity_decode to only use innerHTML of a <p>; added 127 characters in body; added 14 characters in body
Source Link
Marcel Korpel
  • 21.9k
  • 6
  • 63
  • 80
<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>setAttribute test</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 function html_entity_decode(str) {
 var tap = document.createElement("textarea""p");
 tap.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
 return tap.value;innerHTML;
 }
 var theValue = html_entity_decode('A &rarr; B');
 document.getElementById('formula').setAttribute('title', theValue);
 </script>
 </body>
</html>

Credits to The JavaScript Source .

EDIT: I changed the original code to only use innerHTML on a <p> element, instead of innerHTMLandvalue of a <textarea>.

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>setAttribute test</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 function html_entity_decode(str) {
 var ta = document.createElement("textarea");
 ta.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
 return ta.value;
 }
 var theValue = html_entity_decode('A &rarr; B');
 document.getElementById('formula').setAttribute('title', theValue);
 </script>
 </body>
</html>

Credits to The JavaScript Source.

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>setAttribute test</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 function html_entity_decode(str) {
 var p = document.createElement("p");
 p.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
 return p.innerHTML;
 }
 var theValue = html_entity_decode('A &rarr; B');
 document.getElementById('formula').setAttribute('title', theValue);
 </script>
 </body>
</html>

Credits to The JavaScript Source .

EDIT: I changed the original code to only use innerHTML on a <p> element, instead of innerHTMLandvalue of a <textarea>.

added 333 characters in body
Source Link
Marcel Korpel
  • 21.9k
  • 6
  • 63
  • 80

You'll have to use the encoding a document is encoded in. E.g. UTF-8:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTFcharset="utf-8">
 <title>setAttribute test</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 function html_entity_decode(str) {
  var ta = document.getElementByIdcreateElement('formula'"textarea");
 ta.setAttributeinnerHTML = str.replace('title'/</g,"&lt;").replace(/>/g,"&gt;");
 return ta.value;
 }
 var theValue = html_entity_decode('A &rarr; B');
 document.getElementById('formula').setAttribute('title', theValue);
 </script>
 </body>
</html>

Credits to The JavaScript Source .

You'll have to use the encoding a document is encoded in. E.g. UTF-8:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>setAttribute test</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 document.getElementById('formula').setAttribute('title', 'A  B');
 </script>
 </body>
</html>
<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>setAttribute test</title>
 </head>
 <body>
 <img id="formula" src="vote-arrow-up-on.png" alt="A &rarr; C">
 <script>
 function html_entity_decode(str) {
  var ta = document.createElement("textarea");
 ta.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
 return ta.value;
 }
 var theValue = html_entity_decode('A &rarr; B');
 document.getElementById('formula').setAttribute('title', theValue);
 </script>
 </body>
</html>

Credits to The JavaScript Source .

added 4 characters in body
Source Link
Marcel Korpel
  • 21.9k
  • 6
  • 63
  • 80
Loading
Source Link
Marcel Korpel
  • 21.9k
  • 6
  • 63
  • 80
Loading
default

AltStyle によって変換されたページ (->オリジナル) /