JavaScript:如何检查字符是否为RTL? [英] JavaScript: how to check if character is RTL?

查看:245
本文介绍了JavaScript:如何检查字符是否为RTL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式检查浏览器将某些字符作为RTL在JavaScript?

也许创造一些透明的DIV,看着这里的文本放在哪里?



一些上下文。 Unicode 5.2增加了Avestan字母表支持。因此,如果浏览器支持Unicode 5.2,它会将U + 10B00等字符视为RTL(目前只有Firefox支持)。否则,它会将这些字符视为LTR,因为这是默认值。



如何以编程方式检查?我正在写一个Avestan输入脚本,如果浏览器太笨,我想覆盖bidi方向。但是,如果浏览器支持Unicode,比迪烟的设置不应被覆盖(因为这将允许混合斯陀语和斯拉夫语)



我现在这样做:



  var ua = navigator.userAgent.toLowerCase(); 

如果(ua.match( '的webkit')|| ua.match( '急板')|| ua.match( '三叉戟')){
变种输入=的document.getElementById ( '原稿');
if(input){
input.style.direction ='rtl';
input.style.unicodeBidi ='bidi-override';
}
}

但是,显然,这会导致脚本在使用后失效Chrome和Opera开始支持Unicode 5.2。

解决方案

感谢您的评论,但似乎我自己做了这个:

  function is_script_rtl(t){
var d,s1,s2,bodies;

//如果浏览器不支持此,它可能不支持Unicode 5.2
如果(!( getBoundingClientRect 在document.documentElement中))
返回false ;

//设置测试DIV
d = document.createElement('div');
d.style.position ='绝对';
d.style.visibility ='hidden';
d.style.width ='auto';
d.style.height ='auto';
d.style.fontSize ='10px';
d.style.fontFamily ='Ahuramzda';
d.appendChild(document.createTextNode(t));

s1 = document.createElement(span);
s1.appendChild(document.createTextNode(t));
d.appendChild(s1);

s2 = document.createElement(span);
s2.appendChild(document.createTextNode(t));
d.appendChild(s2);

d.appendChild(document.createTextNode(t));

bodies = document.getElementsByTagName('body');
if(bodies){
var body,r1,r2;

body = bodies [0];
body.appendChild(d);
var r1 = s1.getBoundingClientRect();
var r2 = s2.getBoundingClientRect();
body.removeChild(d);

返回r1.left> r2.left;
}

返回false;
}

使用示例:

  Avestan in< script> document.write(is_script_rtl('

How can I programmatically check if the browser treats some character as RTL in JavaScript?

Maybe creating some transparent DIV and looking at where text is placed?

A bit of context. Unicode 5.2 added Avestan alphabet support. So, if the browser has Unicode 5.2 support, it treats characters like U+10B00 as RTL (currently only Firefox does). Otherwise, it treats these characters as LTR, because this is the default.

How do I programmatically check this? I'm writing an Avestan input script and I want to override the bidi direction if the browser is too dumb. But, if browser does support Unicode, bidi settings shouldn't be overriden (since this will allow mixing Avestan and Cyrillic).

I currently do this:

var ua = navigator.userAgent.toLowerCase();

if (ua.match('webkit') || ua.match('presto') || ua.match('trident')) {
    var input = document.getElementById('orig');
    if (input) {
        input.style.direction = 'rtl';
        input.style.unicodeBidi = 'bidi-override';
    }
}

But, obviously, this would render script less usable after Chrome and Opera start supporting Unicode 5.2.

解决方案

Thanks for your comments, but it seems I've done this myself:

function is_script_rtl(t) {
    var d, s1, s2, bodies;

    //If the browser doesn’t support this, it probably doesn’t support Unicode 5.2
    if (!("getBoundingClientRect" in document.documentElement))
        return false;

    //Set up a testing DIV
    d = document.createElement('div');
    d.style.position = 'absolute';
    d.style.visibility = 'hidden';
    d.style.width = 'auto';
    d.style.height = 'auto';
    d.style.fontSize = '10px';
    d.style.fontFamily = "'Ahuramzda'";
    d.appendChild(document.createTextNode(t));

    s1 = document.createElement("span");
    s1.appendChild(document.createTextNode(t));
    d.appendChild(s1);

    s2 = document.createElement("span");
    s2.appendChild(document.createTextNode(t));
    d.appendChild(s2);

    d.appendChild(document.createTextNode(t));

    bodies = document.getElementsByTagName('body');
    if (bodies) {
        var body, r1, r2;

        body = bodies[0];
        body.appendChild(d);
        var r1 = s1.getBoundingClientRect();
        var r2 = s2.getBoundingClientRect();
        body.removeChild(d);

        return r1.left > r2.left;
    }

    return false;   
}

Example of using:

Avestan in <script>document.write(is_script_rtl('
                        

这篇关于JavaScript:如何检查字符是否为RTL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆