3 Replies - 1160 Views - Last Post: 28 January 2013 - 09:48 AM

#1 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Converting utf8 text to html text javascript

Posted 15 January 2013 - 11:02 AM

So I started writing some functions to do this but i cannot be the first one to think of this or what to accomplish this...in javascript.

So for utf8 to html I'm doing something like this

function utf8tohtml(utftxt)
{
        utftxt = utftxt.replace(/&/g,"&")
        utftxt = utftxt.replace(/Á/g,"Á")
        utftxt = utftxt.replace(/É/g,"É")
        utftxt = utftxt.replace(/Í/g,"Í")
        utftxt = utftxt.replace(/Ó/g,"Ó")
        utftxt = utftxt.replace(/Ú/g,"Ú")
        utftxt = utftxt.replace(/Ý/g,"Ý")
        utftxt = utftxt.replace(/ý/g,"ý")
        utftxt = utftxt.replace(/á/g,"á")
        utftxt = utftxt.replace(/é/g,"é")
        return utftxt
}



for html to utf8 i'm doing something like this

function htmltoutf(txt)
{
        txt = txt.replace(/}/,'}');      // right curly brace
        txt = txt.replace(/~/,'~');      // tilde
        txt = txt.replace(//,'');      // box
        txt = txt.replace(/€/,'€');      // Euro
        txt = txt.replace(/ƒ/,'ƒ');      // florin
        txt = txt.replace(/…/,'…');      // ellipsis
        txt = txt.replace(/ˆ/,'ˆ');      // circumflex accent
        txt = txt.replace(/Š/,'Š');      // 
        txt = txt.replace(/Œ/,'Œ');      // capital OE ligature
        return txt
}



and there is more...i've just stopped here for this example...

appreciate the ideas...

This post has been edited by scottyadam: 15 January 2013 - 11:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Converting utf8 text to html text javascript

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: Converting utf8 text to html text javascript

Posted 15 January 2013 - 11:11 AM

I somehow fail to understand the purpose of it.
Was This Post Helpful? 0
  • +
  • -

#3 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: Converting utf8 text to html text javascript

Posted 15 January 2013 - 11:57 AM

Correct me if I am wrong but I thought outputting "text" to a webpage should be done in html tags?   &gt ... etc

Am I wrong and completely out to lunch?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: Converting utf8 text to html text javascript

Posted 28 January 2013 - 09:48 AM

well, the point is that you do not directly do output like for instance PHP does (where you essentially drop text in the appropriate format for the client). In Java​Script it is a little bit different. You do not drop text to the output, you modify the browser’s internal representation of the website (by means of different APIs (e.g. the DOM API)).
So "inserting text in a website" is actually the process of calling the available (resp. the appropriate) API (i.e. JS) to do something.

so to liven up the dry theory by an example, changing the text of a <p> element would be
// changing the first <p> in the document
document.getElementsByTagName("p")[0].textContent = "yo!";

if you want to create new text (resp. new HTML, since the text is a part thereof), you need to follow HTML’s rules of how a document is supposed to be. If that states that text must be enclosed by tags, then you have to create the tags as well.
// create a new <p> element with text and append it to the body
var p = document.createElement("p");
p.textContent = "yo!";
document.body.appendChild(p);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1