View Full Version : Dynamically Changing Text
QuackHead
05-16-2003, 06:46 PM
Here's what I've got... I have a bunch of PDF files that I have available for download in english and french. The user selects their language from a <select> box.
Is there a way to make text in an HTML page change? (i.e. can I use a <label> control or something like that?)
For example, if I have text that says "Brochure 1" when I change the select box it'll change from Brochure 1 to the french translation of that?
Any ideas?
~Quack
scroots
05-16-2003, 07:06 PM
it can be done using <span> tags i think as thats how a javascript clock works.
look at
http://www.javascriptkit.com/script/cut7.shtml
scroots
Danne
05-16-2003, 07:28 PM
If you don't "mount" the select on server or client side, you can do something like this:
<script language="javascript">
var optionWords=[
{en:"Brochure 1",fr:"BrochureInFrench 1"},
{en:"Brochure 2",fr:"BrochureInFrench 2"},
{en:"Brochure 3",fr:"BrochureInFrench 3"},
{en:"Brochure 4",fr:"BrochureInFrench 4"}
];
function setLanguage(lang){
for(var i=0;i<selBrochure.options.length;i++){
selBrochure.options[i].text=optionWords[i][lang];
}
}
</script>
</head>
<body onload="setLanguage('fr')">
<select id="selBrochure">
<option value="1">Brochure 1
<option value="1">Brochure 1
<option value="1">Brochure 1
<option value="1">Brochure 1
</select>
</body>
If you mount it on serverside, you may do something like this (ASP):
<%@ LANGUAGE = JScript %>
<html>
<head>
<title></title>
<%
function printSelBrochure(lang) {
var optionWords=[
{en:"Brochure 1",fr:"BrochureInFrench 1"},
{en:"Brochure 2",fr:"BrochureInFrench 2"},
{en:"Brochure 3",fr:"BrochureInFrench 3"},
{en:"Brochure 4",fr:"BrochureInFrench 4"}
];
var str='<select id="selBrochure">';
for(var i=0;i<optionWords.length;i++){
str+='<option value="1">'+optionWords[i][lang];
}
return str;
}
%>
</head>
<body>
<%=printSelBrochure("en")%>
</body>
</html>
Then you can organize the terms in database for example...
QuackHead
05-16-2003, 07:30 PM
Yes, but the text I want changed ... they're all links, not options in a <select> menu...
Thanks for the URL scroots...
scroots
05-16-2003, 07:42 PM
after a quick rattle round
<form name="thing">
<select name="example" size="1" onChange="updaterrr()">
<option value="FRENCH Brochure 1">Brochire 1 (FR)</option>
<option value="French Brochure 2">Brochire 2 (FR)</option>
</select>
<script language="javascript">
<!--
function updaterrr(){
if (!document.all&&!document.getElementById)
return
thelement=document.getElementById? document.getElementById("tick2"): document.all.tick2
var text=thing.example.options[thing.example.selectedIndex].value
thelement.innerHTML="<B>"+text+"</B>";
}
//-->
</script>
</form>
<span id=tick2>
</span>
scroots
HairyTeeth
05-16-2003, 09:00 PM
Try this:
<html>
<head>
<title>Swap Language</title>
<script type="text/javascript" language="javascript">
<!--;
//constructor;
function linkLang(linkID,en,fr){
this.linkID=linkID;
this.en = en;
this.fr = fr;
}
//'Database' of language equivalents;
var anchorTxt = new Array()
anchorTxt[0] = new linkLang('lnk0','Home','&Agrave; la maison')
anchorTxt[1] = new linkLang('lnk1','Site Map','Carte D`Emplacement')
anchorTxt[2] = new linkLang('lnk2','Links','Liens')
//Swap language function;
function swapLang(fld){
var selectedLanguage=fld.options[fld.selectedIndex].value;
switch(selectedLanguage){
case "Choose":
break
case "en":
for(i=0;i<anchorTxt.length;i++){
document.getElementById('lnk'+i).innerHTML = anchorTxt[i].en;
}
break;
case "fr":
for(i=0;i<anchorTxt.length;i++){
document.getElementById('lnk'+i).innerHTML = anchorTxt[i].fr;
}
break
}
}
//-->
</script>
</head>
<body>
<form>
<select name="langList" onchange="swapLang(this)">
<option value="Choose" selected="selected">Choose...</option>
<option value="fr">French</option>
<option value="en">English</option>
</select>
</form>
<hr />
|
<a id='lnk0' href="javascript: void(0)">Home</a>
|
<a id='lnk1' href="javascript: void(0)">Site Map</a>
|
<a id='lnk2' href="javascript: void(0)">Links</a>
|
<hr />
</body>
</html>
The only thing here is that you will need to give each link a sequential ID, even if the same link occurs elsewhere in the body. For example, the next anchor in the document should be named 'lnk3'.
Also, if the 'Home' link accored, say, at the end of the document as well, and say you had 14 other links before it, the home link at the bottom of the page would be 'lnk15'
See how that goes for starters. I suspect there is a better way of doing this.
Danne
05-16-2003, 09:10 PM
Oh, I totally misunderstood you..
This would work if it's for IE only, because of innerHTML...
<html>
<head>
<title></title>
<script language="javascript">
var words=[
{en:"Brochure 1",fr:"BrochureInFrench 1",file:"br1.pdf"},
{en:"Brochure 2",fr:"BrochureInFrench 2"},
{en:"Brochure 3",fr:"BrochureInFrench 3"},
{en:"Brochure 4",fr:"BrochureInFrench 4"}
];
function setLanguage(lang){
var i=0;
var obj;
while (obj=document.getElementById("aBr"+i)) {
obj.innerHTML=words[i][lang];
i++;
}
}
</script>
</head>
<body onload="setLanguage('en');">
<select onChange="setLanguage(this.value);">
<option value="en" selected>English
<option value="fr">French
</select>
<br>
<a id="aBr0" href="br1.pdf"></a><br>
<a id="aBr1" href="br1.pdf"></a><br>
<a id="aBr2" href="br1.pdf"></a><br>
<a id="aBr3" href="br1.pdf"></a><br>
</body>
</html>
HairyTeeth
05-16-2003, 09:18 PM
Originally posted by HairyTeeth
See how that goes for starters. I suspect there is a better way of doing this.
And Danne has it :)
cheesebagpipe
05-16-2003, 11:21 PM
Originally posted by Danne This would work if it's for IE only, because of innerHTML http://www.developer-x.com/content/innerhtml/
liorean
05-16-2003, 11:32 PM
There are a lot of better arguments against innerHTML than was revealed there, and there are parts of the DOM in the later stages of Working Draft status that will obsolete it. innerHTML is supported by all fifth generation browsers, though. Problem is when you pass from SGML to XML parsing of your XHTML - does innerHTML exist on any element or just when parsing as HTML? Or if you're not using an XHTML technology but some other XML technology?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.