Moving to a modified URL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Normand Peladeau

    #1

    Moving to a modified URL

    I would like to create a link on a web page that will call a script and jump
    to an URL similar but slightly different than the current one. I am doing
    this to allow people to switch languages. For example, no matter where the
    user is on my web site, if he clicks on the FRENCH button at the top, he
    will be moved from:



    to

    http://www.mydomain.com/fr/apage.html

    On this French version of the web page, if he clicks on the ENGLISH button,
    he will be moved back to the:



    Since we have more than 60 pages, we don't want to create 120 links on each
    page. We would prefer to have two scripts, one that will insert a /fr after
    the domain name (to move to the French site), and another one that will
    remove this /fr in order to move to the English version of this page.

    Is this something that can be done with JavaScript?

    Normand P.


  • alu

    #2
    Re: Moving to a modified URL


    "Normand Peladeau" <peladeau@see_o ur_domain.com> wrote
    [color=blue]
    > I would like to create a link on a web page that will call a script and[/color]
    jump[color=blue]
    > to an URL similar but slightly different than the current one. I am doing
    > this to allow people to switch languages. For example, no matter where[/color]
    the[color=blue]
    > user is on my web site, if he clicks on the FRENCH button at the top, he
    > will be moved from:
    >
    > http://www.mydomain.com/apage.html
    >
    > to
    >
    > http://www.mydomain.com/fr/apage.html
    >
    > On this French version of the web page, if he clicks on the ENGLISH[/color]
    button,[color=blue]
    > he will be moved back to the:
    >
    > http://www.mydomain.com/apage.html
    >
    > Since we have more than 60 pages, we don't want to create 120 links on[/color]
    each[color=blue]
    > page. We would prefer to have two scripts, one that will insert a /fr[/color]
    after[color=blue]
    > the domain name (to move to the French site), and another one that will
    > remove this /fr in order to move to the English version of this page.
    >
    > Is this something that can be done with JavaScript?
    >
    > Normand P.[/color]

    _______________ _____________


    Try this:

    --- in a file http://www.mydomain.com/scripts/language.js :

    var loc = document.locati on.toString();

    var english = !!!(loc.search( "/fr/")+1);

    function switchLanguage( ) {
    if(english){
    // switch to french page
    document.locati on = loc.replace(".c om/",".com/fr/");
    }
    else {
    // switch to english page
    document.locati on = loc.replace("/fr/","/");
    }
    }


    ------------------------ test pages:


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html><head>
    <TITLE>apage</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

    <style>
    ..lang {
    text-decoration: underline;
    color:#0000ff;
    }
    </style>

    <script src="http://www.mydomain.co m/scripts/language.js"
    type="text/javascript"></script>


    </head>

    <body style="backgrou nd: #ffffff; margin: 40px;">

    <span class="lang" onClick="switch Language()">eng lish</span>&nbsp;&nbs p;
    <span class="lang" onClick="switch Language()">fre nch</span>
    <br><br>
    <br><br>


    <!--for testing only-->
    <script type="text/javascript">
    document.write( "English content page? " + english)
    </script>
    </body>
    </html>


    _______________ _________
    -alu


    Comment

    • Danny

      #3
      Re: Moving to a modified URL


      Yes, you can do it by using:

      self.location=l ocation.host+'\/fr'+location.pa thname; // to add the
      'fr' from /apage.html

      could also do it by doing a document.URL.re place(...) with a regexp or a
      string, However, the server can do the links by using SSI, and use
      apage.shtml, or even a PHP include(), put the links in it and call them
      from the page.

      Danny

      On Wed, 29 Jun 2005 10:53:20 -0700, Normand Peladeau
      <peladeau@see_o ur_domain.com> wrote:
      [color=blue]
      > I would like to create a link on a web page that will call a script and
      > jump
      > to an URL similar but slightly different than the current one. I am
      > doing
      > this to allow people to switch languages. For example, no matter where
      > the
      > user is on my web site, if he clicks on the FRENCH button at the top, he
      > will be moved from:
      >
      > http://www.mydomain.com/apage.html
      >
      > to
      >
      > http://www.mydomain.com/fr/apage.html
      >
      > On this French version of the web page, if he clicks on the ENGLISH
      > button,
      > he will be moved back to the:
      >
      > http://www.mydomain.com/apage.html
      >
      > Since we have more than 60 pages, we don't want to create 120 links on
      > each
      > page. We would prefer to have two scripts, one that will insert a /fr
      > after
      > the domain name (to move to the French site), and another one that will
      > remove this /fr in order to move to the English version of this page.
      >
      > Is this something that can be done with JavaScript?
      >
      > Normand P.
      >
      >[/color]



      --
      Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

      Comment

      • Dr John Stockton

        #4
        Re: Moving to a modified URL

        JRS: In article <4eBwe.9660$mK5 .613679@news20. bellglobal.com> , dated
        Wed, 29 Jun 2005 13:53:20, seen in news:comp.lang. javascript, Normand
        Peladeau <peladeau@see_o ur_domain.com> posted :[color=blue]
        >I would like to create a link on a web page that will call a script and jump
        >to an URL similar but slightly different than the current one. I am doing
        >this to allow people to switch languages. For example, no matter where the
        >user is on my web site, if he clicks on the FRENCH button at the top, he
        >will be moved from:
        >
        > http://www.mydomain.com/apage.html
        >
        >to
        >
        > http://www.mydomain.com/fr/apage.html
        >
        >On this French version of the web page, if he clicks on the ENGLISH button,
        >he will be moved back to the:
        >
        > http://www.mydomain.com/apage.html
        >
        >Since we have more than 60 pages, we don't want to create 120 links on each
        >page. We would prefer to have two scripts, one that will insert a /fr after
        >the domain name (to move to the French site), and another one that will
        >remove this /fr in order to move to the English version of this page.
        >
        >Is this something that can be done with JavaScript?[/color]

        Yes. Try :

        On English page, button does

        location.href = location.href.r eplace(/(\.com\/)/, "$1fr/")

        On French page, button does

        location.href = location.href.r eplace(/fr\//, "")

        Or similar. Modify for sites that are not .com .

        The buttons (and their code!) can be created by functions EN() and FR()
        in an include file.

        NOTE : you are, of course, being politically offensive; for politeness,
        the French page should use fr/ and the English page en/ in the same
        place.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • alu

          #5
          Re: Moving to a modified URL


          "alu" <none@none.co m> wrote in
          [color=blue]
          > "Normand Peladeau" <peladeau@see_o ur_domain.com> wrote
          >[color=green]
          > > I would like to create a link on a web page that will call a script and[/color]
          > jump[color=green]
          > > to an URL similar but slightly different than the current one. I am[/color][/color]
          doing[color=blue][color=green]
          > > this to allow people to switch languages. For example, no matter where[/color]
          > the[color=green]
          > > user is on my web site, if he clicks on the FRENCH button at the top, he
          > > will be moved from:
          > >
          > > http://www.mydomain.com/apage.html
          > >
          > > to
          > >
          > > http://www.mydomain.com/fr/apage.html
          > >
          > > On this French version of the web page, if he clicks on the ENGLISH[/color]
          > button,[color=green]
          > > he will be moved back to the:
          > >
          > > http://www.mydomain.com/apage.html
          > >
          > > Since we have more than 60 pages, we don't want to create 120 links on[/color]
          > each[color=green]
          > > page. We would prefer to have two scripts, one that will insert a /fr[/color]
          > after[color=green]
          > > the domain name (to move to the French site), and another one that will
          > > remove this /fr in order to move to the English version of this page.
          > >
          > > Is this something that can be done with JavaScript?
          > >
          > > Normand P.[/color]
          >
          > _______________ _____________[/color]


          Apologies - my previously posted script had a flaw; it toggled languages
          regardless of the current page's language. Not good.

          Here's a better solution:


          _________ in a file http://www.mydomain.com/scripts/language.js :


          var loc = document.locati on.toString();

          var english = !(loc.search("/fr/")+1);

          function fr(){
          if(english){doc ument.location = loc.replace(".c om/",".com/fr/")}
          }

          function eng(){
          if(!english){do cument.location = loc.replace("/fr/","/")}
          }


          _______________ ________ test pages:


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html><head>
          <TITLE>apage</TITLE>
          <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

          <style>
          ..lang {
          text-decoration: underline;
          color:#0000ff;
          }
          </style>

          <script src="scripts/language.js" type="text/javascript"></script>


          </head>

          <body style="backgrou nd: #ffffff; margin: 40px; padding: 0px;">


          <span class="lang" onClick="eng()" >english</span>&nbsp;&nbs p;
          <span class="lang" onClick="fr()"> french</span>
          <br><br>
          <br><br>


          <!--for testing only-->
          <script type="text/javascript">
          document.write( "This page's address is:<br>" + document.locati on +
          "<br><br>Englis h content? " + english)
          </script>
          </body>
          </html>

          _______________ _______________ __________

          -alu




          Comment

          Working...