I've this code and it works great on Google Chrome, FireFox & etc..
But there is still a little problem with editing text, for example if I wanna edit a word I can't:
Herdo (so If I want to correct that to Hero I can't just type e instead d in that word it goes at the end of editor so I should copy and paste entire word to replace the current one)
If some one can help me how to fix that works on all browsers I'll be grateful.
Here is the code:
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">
This code works for all browsers and we can type in persian, that's no problem. But the problem is when I'm typing a word in persian and want to edit it the word can't be edited it goes the last of text and don't know how to fix it. if you could try typing a word via that code and just type a sentence and try to edit middle of text you can't edit that because it goes the last of text, so if someone can help me to fix that I'll be grateful
you can't edit that because it goes the last of text, so if someone can help me to fix that
I refer you to the line of code
PHP Code:
ev.target.value +=
this is where you tell your script to ignore the caret position and simply append the text with the new value. If you never need to edit the middle that works just fine. However, if you need to edit the middle you need to
- find the position of the caret
- store the caret position in a variable (IE caretPos)
- insert your transformed symbol at the position of caretPos
the links provided by jmerker will help you to do all of that
There is a similar thread on coding forums that handles that very same issue, except instead of changing languages the OP simply wanted to capitalize the input, but the caret function will work for you just the same:
Quote:
Originally Posted by blaze4218
...try this:
Code:
function c_upper(el){
var caret_position = getCaretPosition(el)
el.value=el.value.toUpperCase()
setCaretPosition(el, caret_position)
}
//http://stackoverflow.com/questions/512528/set-cursor-position-in-html-textbox
function getCaretPosition (el) {
if (el.selectionStart) {
return el.selectionStart;
} else if (document.selection) {
el.focus();
var r = document.selection.createRange();
if (r == null) {
return 0;
}
var re = el.createTextRange(),
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return rc.text.length;
}
return 0;
}
function setCaretPosition(ctrl, pos)
{
if(ctrl.setSelectionRange)
{
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
in that example you would replace the bit about "c_uppper" with your function
Also you could read the whole thread for a little insight about implementation: http://www.codingforums.com/showthread.php?t=240512
__________________
Allwisend bin ich nicht, doch viel ist mir bewursst
-Goethe
How about this one: (it just works with IE, and it's not like before one I can edit middle of text but it doesn't work with firefox and other browsers)
Any Idea to make it works with all browsers?
Code:
<!--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-->
As far as the code in post#8 -- It looks like your overriding an IE only functionality (window.event.keyCode) that's why it fails in other browsers. You should be able to just modify that functionality to accept all browsers "built in" methods:
Code:
<!--Persian Language Script-->
<script language=javascript>
function keyenter(field,e)
{
var key;
if (window.event)
key = window.event ? window.event.keyCode : e.charCode;
if (key>31)
if (key<128)
{
key=' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُيلآ÷ـ،/'د×؛َءٍف'{ًْإ~جژچ^_پشذزیثبلاهتنمئدخحضقسفعرصطغظ<|>ّ'.charCodeAt(key-32);
}
}
</script>
<!--/Persian Language Script-->
but I'm just guessing...
As far as my earlier recommendation I missed a key factor, and that method wouldn't really work (at least not the way I described ).
__________________
Allwisend bin ich nicht, doch viel ist mir bewursst
-Goethe