You could look for the html attribute that specifies this, e.g.
<html dir="ltr" lang="en-US">
In JavaScript you can get this property with:
var dir = document.documentElement.getAttribute('dir');
Of course this relies on the site proving the information.
Update to answer following update to question:
You can pull the text direction for each language from Unicode's CLDR
Using navigator.language
is apparently not reliable. I'd render the HTML page on the back end after detecting preferred languages via the HTTP Accept-Language header. It'll probably be easier to maintain your database of text directions on the server side too.