George Aggelis
George Aggelis

Reputation: 11

How to replace the folder of my url with javascript?

I have read a lot of answers here about changing the site language with jQuery but nothing worked for me.

Well, I have a website as an example: www.domain.com Inside I have folders for the languages. /en/ English pages, /el/ Greek pages. All pages are the same, both for English and Greek. index.html, gallery.html etc

I have two flag icons in the top right header page to change the language. I want, when the user clicks the British flag, to go to /en/page.html and when the user clicks the Greek flag to go to /el/page.html.

<script>
$(document).ready(function(){
   $('a').click(function() {
            document.location.href =document.location.href.replace('/el/' '/en/');
      });
    });
</script>

And here is my html code:

<head>
<a href="javascript:;"><img src="../images/flagen.gif"></a>
</head>

In this example I am on Greek page rootwww/el/index.html and I want to replace /el/ with /en/ folder path and go to /en/index.html

What I am doing wrong?

Upvotes: 1

Views: 1643

Answers (2)

Mehdi Bugnard
Mehdi Bugnard

Reputation: 3979

Did you forget the comma in the "replace" or is this normal for you? And you do not need to write twice "document.location.href ...."

I suggest the following code to test

$(document).ready(function(){
   $('a').click(function() {
            var str = document.getElementById('a').document.location.href;
            var newPath= str.replace("/el/","/en/");
            document.location.href =newPath;
      });
    });

Upvotes: 0

Ketola
Ketola

Reputation: 2767

There's a comma missing from your replace method. Change it to:

<script>
$(document).ready(function(){
   $('a').click(function() {
            document.location.href = document.location.href.replace('/el/','/en/');
      });
    });
</script>

This should work.

Upvotes: 1

Related Questions