Avatar billede fredand Forsker
21. maj 2001 - 17:05 Der er 12 kommentarer og
2 løsninger

Replace ÅÄÖ in a textarea!

Hola!

I need to replace ÅÄÖ in a textArea!

This works fine on both browsers at PC, but I don\'t get this to work at NS on Mac.

I get some texts from a DB where I already replaced the swedish chars \"ÅÄÖ\".

These texts is Stored in an array in the JS-code. I switch between these text through a select-box and applys into a textarea. But I can\'t get it right in the textarrea

I have try this:

function replaceChar(string,text,by)
{
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
   
    if ((strLength == 0) || (txtLength == 0))
    {
        return string;
    }

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength)))
    {
        return string;
    }
   
    if (i == -1)
    {
        return string;
    }

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
    {
        newstr += replaceChar(string.substring(i+txtLength,strLength),text,by);
    }

    return newstr;
}

/*
function decodeSwedishChars(text)
{
    text = replaceChar(text, \'[xax]\', \'å\');
    text = replaceChar(text, \'[xaex]\', \'ä\');
    text = replaceChar(text, \'[xox]\', \'ö\');
    text = replaceChar(text, \'[xAx]\', \'Å\');
    text = replaceChar(text, \'[xAEx]\', \'Ä\');
    text = replaceChar(text, \'[xOx]\', \'Ö\');   
    return text;
}
*/

function decodeSwedishChars(text)
{
    text = replaceChar(text, \'[xax]\', \'&aring\');
    text = replaceChar(text, \'[xaex]\', \'&auml;\');
    text = replaceChar(text, \'[xox]\', \'&oring;\');
    text = replaceChar(text, \'[xAx]\', \'&Aring;\');
    text = replaceChar(text, \'[xAEx]\', \'&Auml;\');
    text = replaceChar(text, \'[xOx]\', \'&Oring;\');   
    return text;
}

The \'[xAx]\',for eg, is my own ecoding for swedish charcters in the DB.

So before i apply the text into the textarea I call the decode-method

        <script language=\"javascript\">   
            var texts = new Array(1);
                var text0 = new Array(2);
                text0[0] = \"2001-05-21 16:38:18.11\";
                text0[1] = decodeSwedishChars(\"<b>V[xaex]lkommen</b>\");
                texts[0] = text0;


            function applyText()
            {
                var form = window.document.forms[\"text_form\"];
                //alert(\"applyText: \" + form.oldtext.selectedIndex);
                for(var i = 0; i < texts.length; i++)
                {
                    if(form.oldtext.options[ form.oldtext.selectedIndex ].value == texts[i][0])
                    {
                        form.text.value = texts[i][1];
                    }
                }
            }
               
        </script>

So if any one have an ide let me know!

Best regards!

Fredrik Andersson
Avatar billede jakoba Nybegynder
21. maj 2001 - 21:02 #1
i string objektet er der en funktion:

replace

Finds a match between a regular expression and a string, and replaces the matched substring with a new substring.
Method of      String
Implemented in  JavaScript 1.2

replace(regexp, newSubStr)

din funktion bliver så til:

function decodeSwedishChars(text) {
    text = text.replace(/\\[xax\\]/g,  \'&aring\');
    text = text.replace(/\\[xaex\\]/g, \'&auml;\');
    text = text.replace(/\\[xox\\]/g,  \'&oring;\');
    text = text.replace(/\\[xAx\\]/g,  \'&Aring;\');
    text = text.replace(/\\[xAEx\\]/g, \'&Auml;\');
    text = text.replace(/\\[xOx\\]/g,  \'&Oring;\'); 
    return text;
};

replace er maskinkodet og langt hurtigere end noget vi kan lave.
læs evt mere om regular expressions på: http://developer.netscape.com/docs/manuals/js/client/jsguide/index.htm

mvh JakobA


Avatar billede fredand Forsker
22. maj 2001 - 08:57 #2
Hola!

Sorry, but did you get this to write out a special char into a TextArea like a \"ä\"? For me I don\'t get this to work.

Best regards!

Fredrik

Avatar billede jakoba Nybegynder
22. maj 2001 - 10:21 #3
For writing in a textarea you should usually use the actual language specific letter, (making sure the default alphabet is in that language)

    text = text.replace(/\\[xax\\]/g,  \'å\');

Det er noget med at de bogstaver der står i felter skives af standard funktioner der tilhører operativsystemet, snarere end af browserens egne \"skriv et bogstav\"-funktioner.

Der er den samme forvirring i dokumentets <title> kommando og i de indbyggede funktioner alert, confirm og prompt.

Mange operativsustemer er baseret på C så det er værd at prøve med C\'s konventioner for specialbogstaver.

Alt ialt prøver jeg at undgå specialbogstaver på den slags steder. Der er ikke nogen konsekvente, pålidelige regler man kan følge.

prøv at teste:
        form.text1.value = \"æøå\";
        form.text2.value = \"&aelig;&oslash;&aring;\";
        form.text3.value = \"\\æ\\ø\\å\";
        form.text4.value = \"\\246\\254\\238\";  //gale numre, find de rigtige
...osv

for at se hvad der virker på din computer. (men du skal ikke forvente det virker på min)       

mvh JakobA
Avatar billede olebole Juniormester
22. maj 2001 - 13:38 #4
<ole>

æ = \\346
ø = \\370
å = \\345

/mvh
</bole>
Avatar billede olebole Juniormester
22. maj 2001 - 13:39 #5
The numbers are octal-values of the ASCII-codes for the characters  :o)
Avatar billede fredand Forsker
22. maj 2001 - 13:54 #6
Hola!

Do you know some site where all this octal-values of the ASCII-codes may be listed?

Fredrik
Avatar billede fredand Forsker
22. maj 2001 - 13:58 #7
A
I found one!

I going to try this now!!!

http://www.geocities.com/Tokyo/Ginza/3379/asciicodes.html
Avatar billede olebole Juniormester
22. maj 2001 - 14:11 #8
Always use escaped \'octaled\' ASCII-values in JavaScript - and you\'ll never run into troubles  ;o)
Avatar billede fredand Forsker
22. maj 2001 - 14:20 #9
Hola!

Soon I will start to cry ;}

I can\'t get it to work in Mac Ns 4.76 when a write it into a textarea.

I\'m I stupid???
function replaceChar(string,text,by)
{
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
   
    if ((strLength == 0) || (txtLength == 0))
    {
        return string;
    }

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength)))
    {
        return string;
    }
   
    if (i == -1)
    {
        return string;
    }

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
    {
        newstr += replaceChar(string.substring(i+txtLength,strLength),text,by);
    }

    return newstr;
}

function decodeSwedishChars(text)
{
    text = replaceChar(text, \'[xax]\', \'\\229\');
    text = replaceChar(text, \'[xaex]\', \'\\228\');
    text = replaceChar(text, \'[xox]\', \'\\246\');
    text = replaceChar(text, \'[xAx]\', \'\\197\');
    text = replaceChar(text, \'[xAEx]\', \'\\196\');
    text = replaceChar(text, \'[xOx]\', \'\\214\');   
    return text;
}

Does this work for you???

Best Regards
Fredrik
Avatar billede olebole Juniormester
22. maj 2001 - 15:02 #10
I guess you are using ASCII-codes....not the octal-values. Try and see here:

http://www.teletechnics.com/reference/ascii_codes.html

Check for instance \'229\' in the left col. Then go to col_3...it says \'345\' - put a \'\\\' in front, and I guess it works  ;o)
Avatar billede fredand Forsker
22. maj 2001 - 15:51 #11
Hola!

I think I have to admit my self defeated by NS in Mac

function decodeSwedishChars(text)
{
    text = replaceChar(text, \'[xax]\', \'\\206\');
    text = replaceChar(text, \'[xaex]\', \'\\204\');
    text = replaceChar(text, \'[xox]\', \'\\224\');
    text = replaceChar(text, \'[xAx]\', \'\\217\');
    text = replaceChar(text, \'[xAEx]\', \'\\216\');
    text = replaceChar(text, \'[xOx]\', \'\\231\');   
    return text;
}

I cant get the ÅÄÖ into the textarea from the DB!

Regarads

Fredrik
Avatar billede olebole Juniormester
22. maj 2001 - 16:16 #12
It looks like you are not using the right \'octals\'. I wrote this little converter for you - try to use it  :o)
    http://www.olebole.f2s.com/index.html
/mvh
Avatar billede olebole Juniormester
29. maj 2001 - 11:43 #13
Didn\'t that help you?
Avatar billede fredand Forsker
31. maj 2001 - 13:31 #14
Hola!

I had to rebuild it an use POST instead of GET, that solved the problem!

Best regards and thanks for all help!!

Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester