can JS pick up LANG attrib's ..?

Discussion in 'Javascript' started by Stuart McDougall, May 29, 2006.

  1. Hi,

    I'm new and naive at JS, but use a lot of modified JS snippets in web
    design. I'm working on a six-language site for a client right now that has
    a small nav bar at the bottom of each template-based page.

    I've had success in previous projects, writing out language-specific nav-bar
    code nested in a document.write statement; a unique .js file for each
    language.

    An idea that I came up with that would allow me to create a function that
    would cover all languages was this: if javascript could somehow detect the
    "lang" attribute of the <html> tag, it could automatically choose which
    "langage" js file to reference for my nav bar.

    Example, if my german nav bar was in a file, "/scripts/de.js" the function
    would pick up "de" from the "lang" attrib in the <html> tag, assign it to a
    variable, say "navlang", and render a document.write statement that would
    "write" out my script source reference as such:

    <script>
    document.write('<script src="/scripts/' + navlang +'.js"></script>');
    </script>

    my problem is I'm not adept enough at JS to write a function that would grab
    the LANG attrib of the page ...

    Can anyone help?

    thanks for any assistance ...

    Stuart
     
    Stuart McDougall, May 29, 2006
    #1
    1. Advertising

  2. Stuart McDougall wrote:

    > I'm working on a six-language site for a client right now that has
    > a small nav bar at the bottom of each template-based page.
    >
    > I've had success in previous projects, writing out language-specific
    > nav-bar code nested in a document.write statement; a unique .js file for
    > each language.
    >
    > An idea that I came up with that would allow me to create a function that
    > would cover all languages was this: if javascript could somehow detect the
    > "lang" attribute of the <html> tag, it could automatically choose which
    > "langage" js file to reference for my nav bar.


    Stop right here. How to navigate if client-side script support is not
    available?

    Why don't you simply use the same server-side script that generated the
    `lang' attribute value of the `html' element to generate the proper
    navigation bar? (You don't use server-side scripting? I see.)

    Yes, it is possible to determine the value of the `lang' attribute of
    the `html' element:

    var lang = document.documentElement.lang;

    But this is part of the /wrong/ approach for i18n.


    PointedEars
    --
    A man who works with his hands is a laborer; a man who works with his
    hands and his brain is a craftsman; but a man who works with his hands
    and his brain and his heart is an artist.
    -- Louis Nizer, lawyer (1902-1994)
     
    Thomas 'PointedEars' Lahn, May 29, 2006
    #2
    1. Advertising

  3. "Thomas 'PointedEars' Lahn" <> wrote in message
    news:...
    > Stuart McDougall wrote:
    >
    >> I'm working on a six-language site for a client right now that has
    >> a small nav bar at the bottom of each template-based page.

    > ................
    > var lang = document.documentElement.lang;
    >


    Thanks Thomas, the var statement worked ok.

    But I'm having trouble writing a statement that will "write" my nav-bar
    reference into the page dynamically. I'm using this:

    <script language="javascript">
    document.write('<script type="text/JavaScript"
    src="../javascripts/navbar/' + NavLang + '.js"></script>');
    </script>

    (I'm using "NavLang" for the variable name, just in case "lang" is a
    reserved word...)

    What this yeilds in my preview window is: " '); "
    in other words, the last 3 trailing punctuation marks of my script.
    but I can't see why it would not work; I've used this type of document.write
    statement many times before, breaking up the html string into 3 parts due to
    a variable in mid-statement.

    can anyone advise?

    thanks,

    Stuart
     
    Stuart McDougall, May 30, 2006
    #3
  4. Stuart McDougall

    MB Guest

    "> But I'm having trouble writing a statement that will "write" my nav-bar
    > reference into the page dynamically. I'm using this:
    >
    > <script language="javascript">
    > document.write('<script type="text/JavaScript"
    > src="../javascripts/navbar/' + NavLang + '.js"></script>');
    > </script>


    Split up the 'script' words in your document.write as those usually cause
    trouble:
    <script language="javascript">
    document.write('<scr'+'ipt type="text/JavaScri'+'pt"
    src="../javascri'+'pts/navbar/' + NavLang + '.js"></scr'+'ipt>');
    </script>
     
    MB, May 30, 2006
    #4
  5. Stuart McDougall

    RobG Guest

    MB wrote:
    > "> But I'm having trouble writing a statement that will "write" my nav-bar
    >> reference into the page dynamically. I'm using this:
    >>
    >> <script language="javascript">


    The language attribute is deprecated, type is required:

    <script type="text/javascript">


    >> document.write('<script type="text/JavaScript"
    >> src="../javascripts/navbar/' + NavLang + '.js"></script>');
    >> </script>

    >
    > Split up the 'script' words in your document.write as those usually cause
    > trouble:


    No, don't. Quote the forward slash in the written script end tag, the
    script engine will ignore it so document.write will write just the
    forward slash '</', the HTML render engine will know not to end the
    outer script tag there:

    ... + NavLang + '.js"><\/script>');


    --
    Rob
     
    RobG, May 30, 2006
    #5
  6. Stuart McDougall wrote:

    > "Thomas 'PointedEars' Lahn" [...] wrote [...]
    >> Stuart McDougall wrote:
    >>> I'm working on a six-language site for a client right now that has
    >>> a small nav bar at the bottom of each template-based page.

    >> ................
    >> var lang = document.documentElement.lang;

    >
    > Thanks Thomas, the var statement worked ok.


    You are welcome.

    > But I'm having trouble writing a statement that will "write" my nav-bar
    > reference into the page dynamically. I'm using this:
    >
    > <script language="javascript">
    > document.write('<script type="text/JavaScript"
    > src="../javascripts/navbar/' + NavLang + '.js"></script>');
    > </script>


    Search the archives for "ETAGO". But as I said already, this is the
    /wrong/ approach (and it will also break in NN4 due to the NRLB).

    > (I'm using "NavLang" for the variable name, just in case "lang" is a
    > reserved word...)


    It is not, and identifiers should not start uppercase unless they refer
    to (intended-to-be) constructors.


    PointedEars
    --
    Alcohol and Math don't mix. So please don't drink and derive!
     
    Thomas 'PointedEars' Lahn, May 30, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Bedford
    Replies:
    1
    Views:
    680
    Ben Edgington
    May 21, 2004
  2. tom c
    Replies:
    5
    Views:
    411
    tom c
    Nov 1, 2006
  3. Jeff Kish
    Replies:
    4
    Views:
    870
    Martin Honnen
    Oct 30, 2008
  4. Mike
    Replies:
    24
    Views:
    230
    Matija Papec
    Sep 22, 2005
  5. Replies:
    3
    Views:
    228
    Roedy Green
    Jul 31, 2012
Loading...

Share This Page