Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Script to change pages in Firefox

  1. #1
    Join Date
    Jan 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Script to change pages in Firefox

    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;
    zxc0<as.length;zxc0++){
    if (as[zxc0].href){
    as[zxc0].href=as[zxc0].href.replace(cng[0],cng[1]);
    }
    }
    }
    //-->
    </script>
    <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.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,072
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    .cnglang {
    color:blue;
    cursor:pointer;
    text-decoration:underline;
    }
    </style>
    <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)
    as[zxc0].href=as[zxc0].href.replace(cng[0],cng[1]);
    }
    </script>
    </head>
    <body>
    <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>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by maidus
    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.
    - Mike

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,072
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by mburt View Post
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    John's method is definitely preferable, although I'd rewrite the actual function:
    Code:
    function CngLinks(from, to) {
      for(var i = 0, a = document.links; i < a.length; ++i)
        a[i].href = a[i].href.replace(from, to);
    }
    And:
    Code:
    <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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by Twey
    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
    - Mike

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You can define "a" within a for-loop?
    The format of a for loop is:
    Code:
    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.
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Thanks Twey, for the very in-depth answer. It took me about half an hour to get through it
    - Mike

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    So if that's the case, this is valid:
    Code:
    for (x = 0, embeds = document.getElementById("target").getElementsByTagName("EMBED");x < embeds.length;x++) {
    	if (x != exception - 1) {
    		embeds[x].stop()
    		}
    	}
    ?
    - Mike

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, but that's using the comma as a statement separator, a rather dubious practice in my opinion. Don't neglect the var keyword.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •