1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, I didn't know that comma was important for replace method.
Indeed it is. The syntax is string.replace(searchval,replaceval). You can also use regular expressions for searching. See w3schools.com/jsref/jsref_replace.asp for examples.
BTW, It's not that comma is 'important for the replace method', a comma is important to separate the parameters in a method call, and replace method takes two parameters, what is to be replaced and the new value. So those two should be separated by a comma, as those two are parameters to the replace method.
0

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;
      });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.