Moving to a modified URL

N

Normand Peladeau

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.
 
A

alu

Normand Peladeau said:
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.

____________________________


Try this:

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

var loc = document.location.toString();

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

function switchLanguage() {
if(english){
// switch to french page
document.location = loc.replace(".com/",".com/fr/");
}
else {
// switch to english page
document.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="http://www.mydomain.com/scripts/language.js"
type="text/javascript"></script>


</head>

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

<span class="lang" onClick="switchLanguage()">english</span>&nbsp;&nbsp;
<span class="lang" onClick="switchLanguage()">french</span>
<br><br>
<br><br>


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


________________________
-alu
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Wed, 29 Jun 2005 13:53:20, seen in Normand
Peladeau said:
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?

Yes. Try :

On English page, button does

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

On French page, button does

location.href = location.href.replace(/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.
 
A

alu

alu said:
____________________________


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.location.toString();

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

function fr(){
if(english){document.location = loc.replace(".com/",".com/fr/")}
}

function eng(){
if(!english){document.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="background: #ffffff; margin: 40px; padding: 0px;">


<span class="lang" onClick="eng()">english</span>&nbsp;&nbsp;
<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.location +
"<br><br>English content? " + english)
</script>
</body>
</html>

________________________________________

-alu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top