Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Bookmarklet, indexOf and if/else

    Hi,
    I am trying to build a bookmarklet to use on the swiss legislation website. All the pages are available in several languages and I want to be able to switch with one button from french to german and from german to french. As an example, the URLs are as follow for the same page:
    in French: http://www.admin.ch/ch/f/rs/311_0/index2.html
    in German: http://www.admin.ch/ch/d/sr/311_0/index2.html

    So I built quite easily two bookmarklets to switch FR-DE and DE-FR:
    Code:
    javascript: var str = window.location.pathname;str = str.replace("f/rs", "d/sr");window.location = str;
    Code:
    javascript: var str = window.location.pathname;str = str.replace("d/sr", "f/rs");window.location = str;
    But when I try to get a single script by using if/else, I cannot manage to have the "else" part working:

    Code:
    javascript: var str = window.location.pathname;
    if (location.pathname.indexOf("d/rs") < 0) {
        str = str.replace("f/rs", "d/sr");
        window.location = str;
    } else {
        str = str.replace("d/sr", "f/rs");
        window.location = str;
    }
    Thinking it might have to do with str.replace, I tried an easier variant, but even this one does not work, the "else" part is never called:

    Code:
    javascript: var str = window.location.pathname;
    if (location.pathname.indexOf("d/rs") < 0) {
        str = str.replace("f/rs", "d/sr");
        window.location = str;
    } else {
        alert('no');
    };
    I would appreciate any help on the matter... thanks in advance, RJ
    Last edited by rajah.bk; 10-05-2010 at 10:07 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Your regex syntax is wrong.

    str=str.replace("f/rs","d/sr");
    should be
    str = str.replace(/f//rs/,"d/sr");
    but this does not work in IE due to a bug concerning forward slashes.

    So try this:-

    Code:
    var str = "http://www.admin.ch/ch/f/rs/311_0/index2.html"
    str = str.replace(/\bf/,"d");  // \b means word boundary
    str = str.replace(/\brs/,"sr");
    alert (str);
    window.location = str;  // go to German site
    
    var str =  "http://www.admin.ch/ch/d/sr/311_0/index2.html";
    str = str.replace(/\bd/,"f");  // \b means word boundary so "d" in admin is ignored
    str = str.replace(/\bsr/,"rs");
    alert (str);
    window.location = str;  // go to French site
    Another simpler way:-

    Code:
    <select id = "language" onchange = "go()">
    <option value = 0>Select Language</option>
    <option value = 1>French</option>
    <option value = 2>German</option>
    </select>
    
    <script type = "text/javascript">
    function go() {
    var val = document.getElementById("language").value;
    if (val == 1) {
    window.location = "http://www.admin.ch/ch/f/rs/311_0/index2.html";  // go to French site
    }
    if (val == 2) {
    window.location = "http://www.admin.ch/ch/d/sr/311_0/index2.html";  // go to German site
    }
    }
    </script>

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
    Last edited by Philip M; 10-05-2010 at 12:21 PM.

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Philip, thanks a lot for your reply. I'll try to use the right regex syntax.
    However your solution does not entirely fit my needs, since I don't need the bookmarklet only for the page I linked, but for all the hundreds of pages of the "systematic collection of laws", which have always identical URLs in french/german but for the "f/rs"-"d/sr" part that distinguishes them. This is why in my first attempt I used window.location (Btw I edited my first post to make the code more readable. Sorry for that.)

    Except the regex syntax, there is a problem with the if...else method, since the two parts of my bookmarklet work when applied separately, but only the first (french->german) works when embedded in a if...else structure.

    Also, I tried to integrate your suggestion as to the regex syntax in my original bookmarklet (I'm not concerned with IE compatibility because my target are mobile phones) but as a result even the first tier does not work anymore:

    Code:
    javascript: var str = window.location.pathname;
    if (location.pathname.indexOf("d/rs") < 0) {
        str = str.replace(/f//rs/, "d/sr");
        window.location = str;
    } else {
        str = str.replace(d//sr/,"f/rs");window.location=str;}
    So I'm back with my original code:

    Code:
    javascript: var str = window.location.pathname;
    if (location.pathname.indexOf("d/rs") == -1) {
        str = str.replace("f/rs", "d/sr");
        window.location = str;
    } else {
        str = str.replace("d/sr", "f/rs");
        window.location = str;
    }
    Thanks once more for your help...
    Last edited by rajah.bk; 10-05-2010 at 10:08 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    If you want the user to select a language, and thereafter all the pages on your site which he visits are presented in that language, use a cookie - session cookie or persistent cookie as you prefer.

    javascript: is quite redundant.


  •  

    Posting Permissions

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