最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get the dir attribute of the <html> element in JavaScript (RTL or LTR) - Stack Overflow

programmeradmin5浏览0评论

I am working rtl right to left direction. I want to know how to get dir value in alert using javascript.

Here is my html code

<html dir="rtl">
</html>

Here is my javascript code

alert(document.getElementsByTagName("html")[0]);

I am not getting the alert as dir ="rtl".

Based on the direction I have to do some condition checking. Instead of attribute is there any way please help me

Please help me how to get that tag name.

I am working rtl right to left direction. I want to know how to get dir value in alert using javascript.

Here is my html code

<html dir="rtl">
</html>

Here is my javascript code

alert(document.getElementsByTagName("html")[0]);

I am not getting the alert as dir ="rtl".

Based on the direction I have to do some condition checking. Instead of attribute is there any way please help me

Please help me how to get that tag name.

Share Improve this question edited Sep 25, 2024 at 11:03 Flimm 151k49 gold badges274 silver badges288 bronze badges asked Mar 3, 2015 at 12:47 Maha1419Maha1419 1911 gold badge2 silver badges11 bronze badges 1
  • Have you tried getAttibute? Something like.. document.getElementsByTagName("html")[0].getAttribute('dir') – Rakesh_Kumar Commented Mar 3, 2015 at 12:49
Add a ment  | 

3 Answers 3

Reset to default 9

Thanks for the reply. Here is what i Did

if (document.dir == "rtl"){
 function name()
}else{
function name1()
}

Regards Mahadevan

This snippet will get data directly from html element

document.getElementsByTagName("html")[0].getAttribute("dir");

you can get this more easily from the attributes of the document Document.dir

var direction;
(document.dir !=undefined)? direction =document.dir : direction =document.getElementsByTagName("html")[0].getAttribute("dir");

if (direction=="rtl") {
//do something
}
else {
//do something else
}
发布评论

评论列表(0)

  1. 暂无评论