Reputation: 157
I created a bilingual website using two databases:
www.martyregan.com/ www.martyregan.com/jp/
You can choose the language of the website using the 'Website Language' flags, but currently the links only bring you to the homepage. The paths/URLs on both sites are exactly the same, other than /jp/ directory on the Japanese site.
I'm looking for a way to alter the hyperlinks to go to the parallel page, based on the URL of the page the visitor is currently on. I figure it'd be quite simple being that the paths are identical, but not really sure where to start with my little knowledge of jquery.
Upvotes: 1
Views: 484
Reputation: 1185
This assumes your language is accessible as seen below. "http://" is removed from the URL for convenience.
$(function(){
var lang = 'jp';
$('a').attr('href', function(x, url){
var split = url.replace(/(http:)?(\/\/)?/, '').split('/');
return split.shift() + '/' + lang + '/' + split.join('/');
});
});
Upvotes: 1
Reputation: 191729
You may be able to get away with using the <base>
tag depending up on the browsers you need to support. You can just set that to whatever the base is by acquiring it from the server.
If you want to use jQuery to do it, it should be fairly simple if all of your href
s are absolute:
$("a").attr('href', function (_, href) {
return $("base").attr('href') + href;
});
This assumes that you are using <base>
. If you're not you can get the path from window.location.path
, or even some other element on the page.
In case you are confused, the JavaScript is only required if <base>
is not enough to work on its own
Upvotes: 0