Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Embed text from .txt file into HTML using JS

    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

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,753
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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 site had loads of special chars, the one i saw you posted was &aelig; (Š)

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    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 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

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,371
    Thanks
    11
    Thanked 591 Times in 572 Posts
    your editor can leave the raw codes intact, just convert it right before you set .innerHTML or call document.write();

    Code:
    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)" ...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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.

  • #6
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    your editor can leave the raw codes intact, just convert it right before you set .innerHTML or call document.write();

    Code:
    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.

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Did you try to just use UTF-8? You don't need any escaping for that.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,371
    Thanks
    11
    Thanked 591 Times in 572 Posts
    Quote Originally Posted by Cltornes View Post
    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.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #9
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    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?

  • #10
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    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

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,371
    Thanks
    11
    Thanked 591 Times in 572 Posts
    one last time: if you post the code, i will help you.

    this includes all your html, and the contents of forside_tekst.txt
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #12
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    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

  • #13
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,371
    Thanks
    11
    Thanked 591 Times in 572 Posts
    Code:
    document.write (  escapeExtended(   '┼h abe.'   ));
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    Cltornes (07-22-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •