can JS pick up LANG attrib's ..?

S

Stuart McDougall

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
 
T

Thomas 'PointedEars' Lahn

Stuart said:
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
 
S

Stuart McDougall

Thomas 'PointedEars' Lahn said:
................
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
 
M

MB

"> 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>
 
R

RobG

MB said:
"> But I'm having trouble writing a statement that will "write" my nav-bar

The language attribute is deprecated, type is required:

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>');
 
T

Thomas 'PointedEars' Lahn

Stuart said:
"Thomas 'PointedEars' Lahn" [...] wrote [...]
................
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top