Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating HTML escape charaters? 1

Status
Not open for further replies.

gruvn

Programmer
Oct 15, 2004
24
CA
Hey all,
I think everyone's seen those big tables out there listing the ecsaped versions of various non-standard characters. I'm talking about things like:
"
&
ʼn

What kind of encoding is this, (and if you know that), could a bit o' javascript be developed to generate the escaped versions automatically? For example, create a text box, and when I type in " and hit go, it returns "

I've done some searching, but haven't quite found what I'm looking for. Any of you brainiacs care to give it a go?

M
 
Something like this (insert manually though)
Code:
var unescaped = new Array('"'," ","<",">") //etc
var escaped = new Array("&quot;","&nbsp;","&lt;","&gt;"); //etc


function indexof(elem)
{
  for (var i=0; i<this.length; i++)
  {
    if (this[i]==elem)
      return i;
  }
  return -1;
}
Array.prototype.indexOf = indexof;

function encode(source, target)
{
  var str = source.value;
  var result = "";
  for (var i=0; i<str.length; i++)
  {
    var idx = unescaped.indexOf(str.charAt(i));
    if (idx == -1)
      result += str.charAt(i);
    else
      result += escaped[idx];
  }
  target.value = result;
}

// -->
</script>
</head>
<body>
<form>
 <label for="input">Input</label>
 <textarea name="input"></textarea>
 <br>
 <label for="output">Output</labe>
 <textarea name="output"></textarea>
  <br>
  <input type="button" value="Convert" onclick="encode(this.form.elements['input'], this.form.elements['output']);">
</form>
</body>
</html>
Not tested though...

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakesperean sonnet.
 
That was an amazingly fast response (and it works!),

So is the only way to do this by knowing in advance what all of the escape characters are, and plugging them into the array? I guess I was thinking of this bit of code I saw earlier that converts back and forth between the encoding used in URLs.

They look fundamentally different. Like the example below is ASII to ISO or something, while the escaped charaters used by HTML are inherent to HTML only?

Thanks!

M

<script type="text/javascript">
<!--
function convert2hex() {
query = document.theForm.query.value
var ans = escape(query)
document.theForm.response.value = ans
highlight()
}

function convert2txt() {
query = document.theForm.query.value
var ans = unescape(query)
document.theForm.response.value = ans
highlight()
}

function highlight() {
document.theForm.query.focus()
document.theForm.query.select()
}
-->
</script>
</head>

<body onLoad="highlight()">
<H2 ALIGN="CENTER" CLASS="grn">URL Encode & Decode</H2>

<FORM name="theForm">
<h4 class="emph">Enter a character or string that you want to convert to, or from, hexadecimal encoding: <br><input type="text" name="query" size="60"></h4>
<h4 class="emph">This is the result: <br><input type="text" name="response" size="60"></h4>
<input type="button" onClick="convert2hex()" value="Encode">
<input type="button" onClick="convert2txt()" value="Decode">&nbsp;<input type="reset" onClick="highlight()">
</FORM>
 
[tt]escape()[/tt] and [tt]unescape()[/tt] are built-in javascript functions that convert into and from invalid URL characters into their hex ascii (" " is %20, (2*16+0*1 = 32), etc.). I don't believe there is a unicode converter but if there is, this would be much easier!

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakesperean sonnet.
 
Scratch that, forgot about [tt]charCodeAt()[/tt].
How about this?
Code:
<script type="text/javascript">
<!--

var invalid = '(){}[]<>"&©'; // problematic characters

function encode(source, target)
{
  var str = source.value;
  var result = "";
  for (var i=0; i<str.length; i++)
  {
    var idx = invalid.indexOf(str.charAt(i));
    if (idx == -1)
      result += str.charAt(i);
    else
      result += "&#" + invalid.charCodeAt(idx) + ";";
  }
  target.value = result;
}

// -->
</script>
This only converts to unicode, not those built-in shortcuts (&amp;, &copy;, etc.).

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakesperean sonnet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top