469,891 Members | 2,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,891 developers. It's quick & easy.

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:

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.
Jul 23 '05 #1
4 3216
alu

"Normand Peladeau" <peladeau@see_our_domain.com> wrote
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
Jul 23 '05 #2

Yes, you can do it by using:

self.location=location.host+'\/fr'+location.pathname; // to add the
'fr' from /apage.html

could also do it by doing a document.URL.replace(...) 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_our_domain.com> wrote:
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.


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #3
JRS: In article <4e*******************@news20.bellglobal.com>, dated
Wed, 29 Jun 2005 13:53:20, seen in news:comp.lang.javascript, Normand
Peladeau <peladeau@see_our_domain.com> posted :
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.

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

"alu" <no**@none.com> wrote in
"Normand Peladeau" <peladeau@see_our_domain.com> wrote
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.


____________________________

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


Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Normand Peladeau | last post: by
2 posts views Thread by Carl Gilbert | last post: by
8 posts views Thread by rick | last post: by
reply views Thread by linkswanted | last post: by
12 posts views Thread by =?Utf-8?B?TWFyaw==?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.