...

View Full Version : Embed text from .txt file into HTML using JS



Cltornes
07-15-2011, 10:34 AM
Hi

Im new to JS. Im having a problem with special character (im from Denmark where we use ). When embeded the text doesnt include the special characters. I know why but I dont know how to resolve it.


My HTML looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


</head>
<body>
<div id="container">

<div id="text-top">
<script language="javascript" src="http://www.example.dk/upload/forside_tekst.txt"></script>
</div>

</div>
</body>
</html>

The .txt file looks like this:

document.write('Example: h abe. ');


Please help me :)

alykins
07-15-2011, 11:37 AM
can you convert the .txt char to the html equivs? or do a compare before writing and if they are special chars, replacing them?
this (http://www.web-source.net/symbols.htm) site had loads of special chars, the one i saw you posted was &aelig; ()

Cltornes
07-15-2011, 12:04 PM
can you convert the .txt char to the html equivs? or do a compare before writing and if they are special chars, replacing them?
this (http://www.web-source.net/symbols.htm) site had loads of special chars, the one i saw you posted was &aelig; ()

First of all thanks for your answer :)

There might be a solution here. The problem with converting them is, that the journalist who is writing the text has no HTML experience s telling him to write equivs is a no go. But maybe a solution with identifying and replacing them would do the job. But how I dont now :)

rnd me
07-15-2011, 08:34 PM
your editor can leave the raw codes intact, just convert it right before you set .innerHTML or call document.write();



function escapeExtended(s){
return s.replace(/([\x80-\xff])/g, function (a, b) {
var c = b.charCodeAt();
return "&#" + b.charCodeAt()+";"
});
}





edit wherever you passed the text before, let's call it a variable named text.
so, instead of "text", you now say "escapeExtended(text)" ...

devnull69
07-16-2011, 07:48 PM
Are you serious about that?

The better solution (by far) would be to have all the files in UTF-8 (preferrably without BOM). You can use an editor like notepad++ to achieve that even for raw text files. The OP should just make sure that the following files are all in UTF-8

a) the file with the HTML code you showed in your first post
b) the txt file you want to embed ... this is actually a Javascript file with .txt extension. For readability's sake you should rename it to .js

To achieve this in Notepad++ you should call the menu "format/utf-8 without BOM" before starting to enter text to the file. If you change the format afterwards you will have to re-enter all the special characters.

Cltornes
07-18-2011, 07:55 AM
your editor can leave the raw codes intact, just convert it right before you set .innerHTML or call document.write();



function escapeExtended(s){
return s.replace(/([\x80-\xff])/g, function (a, b) {
var c = b.charCodeAt();
return "&#" + b.charCodeAt()+";"
});
}





edit wherever you passed the text before, let's call it a variable named text.
so, instead of "text", you now say "escapeExtended(text)" ...

Ok. Thanks for your answer :) But im not sure how to use this. I cant get it to work.

I tried this:

<div id="text-top"> <script language="javascript" escapeExtended('http://www.stark.dk/upload/facebook/tekst/forside_tekst.txt')</script> </div>

But it is wrong. Im not so good to see the connection between HTML and JS.

devnull69
07-18-2011, 03:40 PM
Did you try to just use UTF-8? You don't need any escaping for that.

rnd me
07-18-2011, 09:01 PM
Ok. Thanks for your answer :) But im not sure how to use this. I cant get it to work.

I tried this:

<div id="text-top"> <script language="javascript" escapeExtended('http://www.stark.dk/upload/facebook/tekst/forside_tekst.txt')</script> </div>

But it is wrong. Im not so good to see the connection between HTML and JS.

post the contents of your embeded script and i'll show you what to do.

Cltornes
07-19-2011, 08:00 AM
post the contents of your embeded script and i'll show you what to do.

Hi :)

All I have is the code in the top. I know how to insert the function in a JS tag in the head. But I dont know how og where to put the escapeExtended?

Cltornes
07-19-2011, 08:05 AM
Did you try to just use UTF-8? You don't need any escaping for that.

Hi Devnull69

Thanks for the help. I will try with the other solution first. Im affraid that the editor might damage the UTF in the file somehow. Therefore I want to make sure that it is convertet with JS.

But thanks a lot for the help :)

rnd me
07-19-2011, 05:43 PM
one last time: if you post the code, i will help you.

this includes all your html, and the contents of forside_tekst.txt

Cltornes
07-21-2011, 11:36 AM
one last time: if you post the code, i will help you.

this includes all your html, and the contents of forside_tekst.txt

Hi RND. Here is the code:

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script>
function escapeExtended(s){
return s.replace(/([\x80-\xff])/g, function (a, b) {
var c = b.charCodeAt();
return "&#" + b.charCodeAt()+";"
});
}
</script>

<style type="text/css">
body {
width:520px;
margin:0; padding:0; border:0;
background-color:#f1f1f1;
background-repeat:no-repeat;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}

#container {
background-color:#f1f1f1;
width:520px;
height:600px;
}

#text-top {
margin-left:27px;
margin-right:27px;
margin-top:20px;
height:150px;
}

</style>

</head>
<body>
<div id="container">

<div id="text-top">
<script language="javascript" src="/tekst.txt"></script>
</div>

</div>
</body>
</html>


Txt file:
document.write('h abe.');

Thats all my code. Hope that is what you want. I might have misunderstood you :)

rnd me
07-21-2011, 06:19 PM
document.write ( escapeExtended( 'h abe.' ));



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum