View Full Version : Script to change pages in Firefox

01-03-2007, 03:53 PM
Hi all,

I have a compatability problem with this script that I'm using for Firefox. It's a script that allows someone who's viewing a page in a particular language (ex: English) to click on a button and it changes to the exact same page located in another directory on the webserver but in another language (ex: Spanish). An example of how it would be used would be on this page: www.united.com.br. There is a link on top to change languages between English and Portugues. The only problem with this one is if you are navigating the site and have reached a page and want to change the language there - you can't. This button will take you back to the index but in the other language. I want the same thing except that it stays on the same page. I found a script that does this but it only works in IE - not FF. Here's the script:

<script language="JavaScript" type="text/javascript">
function CngLinks(obj){
var as=document.getElementsByTagName("A");
var cng=obj.value.split("|");
for (var zxc0=0;
if (as[zxc0].href){
<span onclick="CngLinks(this);" value="espanol|english"><a href="#">English</a></span>&#160;&#160;<span class="headerpipe">|</span>&#160;&#160;

Does anyone have any suggestions?

Alfred Smith.

01-04-2007, 06:30 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.cnglang {
<script type="text/javascript">
function CngLinks(str){
var as=document.getElementsByTagName("A");
var cng=str.split("|");
for (var zxc0=0;zxc0<as.length;zxc0++)
if (as[zxc0].href)
<span class="cnglang" onclick="CngLinks('espanol|english');">English</span> <span class="headerpipe">|</span> <span class="cnglang" onclick="CngLinks('english|espanol');">Espa&ntilde;oles</span><br>&nbsp;<br>
<a href="espanol/bob.htm">Bob</a>

01-04-2007, 12:13 PM
var cng=obj.value.split("|");
obj is assigned to the indexs of the links, you can't split that, and say if it was even obj[0], a links can't have the property "value". That's where your error is coming from.

01-04-2007, 05:20 PM
obj is assigned to the indexs of the links, you can't split that, and say if it was even obj[0], a links can't have the property "value". That's where your error is coming from.

No, everything was assigned properly in the script. Giving a span a value attribute is invalid markup but, might be OK for scripting. Nesting a link which is set to href="#" within it is very questionable. Typically, this should reload the page and then, any changes made by the script would be lost. The original code might be able to be made to work simply by adding a:

;return false

to the event call in the span or by moving it (with the return false) and the value attribute to the link.

Still, it is better to rewrite it as I have in my previous post, so as to validate and so as not to cause page reloads in non-javascript enabled browsers.

01-04-2007, 06:07 PM
John's method is definitely preferable, although I'd rewrite the actual function:
function CngLinks(from, to) {
for(var i = 0, a = document.links; i < a.length; ++i)
a[i].href = a[i].href.replace(from, to);
<span class="cnglang" onclick="CngLinks('espanol', 'english');">English</span>However, there are bigger problems: this sort of thing should never be done client-side. It's easy enough to do server-side, and doesn't require Javascript. Also, server-side it's possible to set the default appropriately by the user's Accept-Language.

01-04-2007, 07:57 PM
var i = 0, a = document.links
You can define "a" within a for-loop? Cool... You're good at saving space. :)
Another question though...
What's with the "++i"? Shouldn't it be "i++"? As a increment operator, or something like that

01-04-2007, 08:11 PM
You can define "a" within a for-loop?The format of a for loop is:
for (initialisation; test; modification)Each is a single statement. Since the var keyword allows multiple variables to be defined with a single statement, it's possible to define as many variables as are required in the initialisation statement (the only one of the three in which var is allowed).
What's with the "++i"? Shouldn't it be "i++"? As a increment operator, or something like that++ has two functions: as a pre-increment operator (++myvar) and a post-increment operator (myvar++). They both increment the variable, but their return values differ: the pre-increment operator returns the value as it is after the incrementation, while the post-increment operator returns the value as it was before incrementation.
var a = 2, b;
b = ++a;
// a == 3; b == 3;
a = 0;
// a == 0; b == 3;
a = b++;
// a == 3; b == 4;The same applies with the pre- and post-decrement operators (--).

For the purposes of a for loop there is no effective difference; however, the post-increment operator is less memory-efficient in some old compilers (it needs another variable to store the original value, think about it). Modern compilers optimise this away, but it's still a good habit.

01-04-2007, 08:23 PM
Thanks Twey, for the very in-depth answer. It took me about half an hour to get through it :p

01-04-2007, 08:26 PM
So if that's the case, this is valid:

for (x = 0, embeds = document.getElementById("target").getElementsByTagName("EMBED");x < embeds.length;x++) {
if (x != exception - 1) {


01-04-2007, 08:38 PM
Yes, but that's using the comma as a statement separator, a rather dubious practice in my opinion. Don't neglect the var keyword.

01-04-2007, 08:57 PM
Ah, yes. I forgot that, silly mistake.