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 12 of 12
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I set this script to work for firefox or other browsers?

    Hello
    I have this Persian language script for text box.. but it works just for Internet Explorer.. how can I change code and make it to work for Firefox or other browsers such as Google Chrome, Opera and so on...
    this is the script:

    <!--Persian Language Script-->
    <script language=javascript>
    function keyenter(field,e)
    {
    var key;
    if (window.event)
    key = window.event.keyCode;

    if (key>31)
    if (key<128)
    {
    if (window.event)
    window.event.keyCode=' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُيلآ÷ـ،/'د×؛َءٍف'{ًْإ~جژچ^_پشذزیثبلاهتنمئدخحضقسفعرصطغظ<|>ّ'.charCodeAt(key-32);
    }
    }
    </script>
    <!--/Persian Language Script-->


    if anyone know plz tell me..
    Thanks

  • #2
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    375
    Thanks
    9
    Thanked 55 Times in 54 Posts
    Hello Ersa,
    welcome to the club

    Try a quick & dirty one below:
    PHP Code:
    <html>

    <
    head>

    <
    style type="text/css">
      
    #test{
        
    width320px;
        
    height240px;
        
    border1px solid black;
      }
    </
    style>

    <!--
    Persian Language Script-->
    <
    script type="text/javascript">
      var
        
    arabic =' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُيلآ÷ـ،/'+"'"+'د×؛َءٍف'+"'"+'{ًْإ~جژچ^_پشذزیثبلاهتنمئدخحضقسفعرصطغظ<|>ّ';

      function 
    keyenter(ev){
        var
          
    akey parseInt(ev.charCode);
        if (!
    isNaN(akey) && akey>31 && akey<128){
          
    document.getElementById("test").innerHTML += arabic[akey-32];
        }
      }

    </script>
    <!--/Persian Language Script-->
    </script>

    </head>

    <body onkeypress="keyenter(event)">
      <div id="test"></div>
    </body>
    </html> 

  • #3
    Registered User
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks..
    But I have my own text box and I want same code just work for other browsers... this one is not working
    Thanks pal..

  • #4
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    375
    Thanks
    9
    Thanked 55 Times in 54 Posts
    Sorry, I haven't actually test my demo to other browsers.

    The one below has been tested thru my Opera, FF, IE, Safari and Chrome,
    let's see if it'll pass test thru yours this time.
    And I've put keyenter() event handler to onkeydown event.

    One other thing,
    your keyenter(field,e) function store the result to window.event.keyCode.
    It might be possible in IE, but in other browsers I don't think it could (event.keyCode is readonly in other browsers).

    So I'll put the result to a global var result instead:

    PHP Code:
    <!--Persian Language Script-->
    <
    script type="text/javascript">
      var
        
    arabic =' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُيلآ÷ـ،/'+"'"+'د×؛َءٍف'+"'"+'{ًْإ~جژچ^_پشذزیثبلاهتنمئدخحضقسفعرصطغظ<|>ّ';
        
    result ='';

      function 
    keyenter(ev){
        var
          
    acode window.event window.event.keyCode ev.keyCode;
        if (
    acode>31 && acode<128){
          
    result acode;
          
    // to get the arabic character, use
          // result = String.fromCharCode(arabic.charCodeAt(acode-32));
        
    }
      }

    </script>
    <!--/Persian Language Script--> 

  • #5
    Registered User
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank u so much MyFriend..

    could u please tell me for text box what should I put for onkeypress


    PHP Code:
    <textarea name="postText" id="postText" cols="38" rows="10" class="textForm postingForm" tabindex="2" onkeypress="keyenter(event.keyCode)">{$postText}</textarea
    I put this but this works just for first code I've given..
    so for your new code what keyenter should I put?
    thanks

  • #6
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    375
    Thanks
    9
    Thanked 55 Times in 54 Posts
    My last example used onkeydown event handler,
    and also I've used a <div> to store the result.

    Using <textarea> would be a bit complicated,
    since we have to deal with event prevention.

    Hope this one works thru your browsers:
    Code:
    <html>
    <head>
    <style type="text/css">
      #postText{
        font-size: 200%;
      }
    </style>
    
    <script type="text/javascript">
      var
        arabic =' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُيلآ÷ـ،/'+"'"+'د×؛َءٍف'+"'"+'{ًْإ~جژچ^_پشذزیثبلاهتنمئدخحضقسفعرصطغظ<|>ّ';
    
      function keyenter(ev){
        var
          acode = window.event ? window.event.keyCode : ev.charCode;
        if (acode>31 && acode<128){
          if (ev.target){
            ev.target.value += 
              String.fromCharCode(arabic.charCodeAt(acode-32));
            ev.preventDefault();
          }
          else{
            window.event.keyCode = arabic.charCodeAt(acode-32);
          }
        }
      }
    
    </script>
    
    </head>
    
    <body>
      <textarea name="postText" id="postText" cols="38" rows="10" class="textForm postingForm" tabindex="2" onkeypress="keyenter(event)">{$postText}</textarea>
    </body>
    </html>
    Last edited by hdewantara; 06-26-2010 at 05:49 PM.

  • #7
    Registered User
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much My friend.
    That works Great now...

  • #8
    Registered User
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have one more question..
    that's working good now..
    but there is a little problem..
    when someone write in persian and wants to edit the written text.. it can't be.. it goes at the last of text.. so we can not edit the word if it is on the begining or middle.. when we write a word or sentence.. we cant change it if we want to highlight on it and change it.. it goes to the last of the text.. that's strange for me.. but this is just for other browsers.. for Internet Explorer it's ok and good.

  • #9
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    375
    Thanks
    9
    Thanked 55 Times in 54 Posts
    Ersa, all I could see that...

    a) in IE, I should type left keypad to move right, or right to move left.
    On the contrary, FF responds like normal western keyboard.

    b) Both my IE and FF works well in either deleting (erasing character to left),
    or back-spacing (erasing character to right). This is the reverse to normal western keyboard.

    Why point a) differs? I think the author has inserted the character for left, right, delete, backspace keys, etc...
    into the array arabic variable, and different browsers reacts differently to this char output.

    So which one you would choose? A persian editor with western navigation ?
    For quick solution in other browsers than IE, try detect, find & exchange 2 right chars in the array, for reverse respond.
    The proper solution would be to use event.which, instead of event.keycode or event.charcode, I think...

    Hendra
    Last edited by hdewantara; 06-30-2010 at 11:43 AM.

  • #10
    Registered User
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not familier with these codes..
    but I want it be Persian and Right to left..
    it just ok with IE.. but for other browsers as I told.. it has little problem..
    if you know what I mean.. Please help me on this..
    Thank you so much..

  • #11
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    375
    Thanks
    9
    Thanked 55 Times in 54 Posts
    Ersa,

    Building an editor would require more complex logic,
    and we were wrong all this time,
    always trying to add persian character at the end of textarea buffer.

    That's why you can't edit in the middle.

    Moreover, different browser turns out to have different key-response mechanism.
    Sadly I could only build one working on Firefox, but not on Safari, Chrome nor Opera.

    So here it is, totally different from previous version:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    >
    <
    html xmlns="http://www.w3.org/1999/xhtml">

    <
    head>

    <
    meta content="text/html; charset=utf-8" http-equiv="content-type" />

    <
    style type="text/css">
      
    #postText{
        
    font-size200%;
      }
    </
    style>

    <
    script type="text/javascript">
    //<![CDATA[
      
    var
        
    timer null;
        
    isDefault true;
        
    isCtrl false;
        
    isAlt false;
        
    isShift false;
        
    isMeta false;
        
    charCode 0;
        
    persian_array 
          
    ' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُيلآ÷ـ،/' +
          
    "'" 'د×؛َءٍف' +
          
    "'" '{ًْإ~جژچ^_پشذزیثبلاهتنمئدخحضقسفعرصطغظ<|>ّ';

      function 
    keyenter(ev){
        if (
    ev.target){        // ONLY FOR FF:
          
    if (isDefault){    
            if ( (
    ev.charCode>31) && (ev.charCode<128) ){
              
    ev.preventDefault();    // cancel default action.

              
    isDefault false;    // prepare to resend custom keypress event, & let it pass default keypress processor.
              
    isCtrl ev.ctrlKey;
              
    isAlt ev.altKey;
              
    isShift ev.shiftKey;
              
    isMeta ev.metaKey;
              
    charCode persian_array.charCodeAt(ev.charCode-32);

              
    timer setTimeout(    //recall by hook function.
                
    function(){
                  var
                    
    el document.getElementById("postText"); 
                    
    ev2 document.createEvent("KeyboardEvent");
                  
    ev2.initKeyEvent"keypress"truetruenullisCtrlisAltisShiftisMeta0charCode );
                  
    el.dispatchEvent(ev2);
                }, 
    10);
            }
          }
          else{            
    // pass modified keypressed, restore status.
            
    isDefault true;
          }
        }
        else{        
    // ONLY FOR IE:
          
    var 
            
    code window.event.keyCode;
          if ( 
    code>31 && code<128 ){
            
    window.event.keyCode persian_array.charCodeAt(code-32);
          }
        }
      }

    //]]>
    </script>

    </head>

    <body>
      <textarea name="postText" id="postText" cols="38" rows="10" 
        class="textForm postingForm" tabindex="2" onkeypress="keyenter(event)">{$postText}</textarea>
    </body>
    </html> 
    Hint: you still can't use Ctrl-C, Ctrl-X nor Ctrl-V for copy, cut or pasting.
    Use Ctrl-Ins, Shift-Del, or Shift-Ins instead. I don't know why.

    Hmm, you should get more advises for this to work in other browsers,
    by changing your thread title to something more specific like:
    "A cross browser persian editor, please...",

    That I believe would invite our friends at codingforums,
    for giving more thorough advise.

    Good luck.
    Last edited by hdewantara; 07-05-2010 at 01:21 AM.

  • #12
    Registered User
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ya.. it works with IE and firefox...
    Thank you so much My Friend..


  •  

    Tags for this Thread

    Posting Permissions

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