1

I have 2 days trying to figure this out. I want to reloasd the url and if you click again dont append the same thing

I am trying to change the url and reload the page using a function but it is worthless.

I am always getting the url without refreshing the site or somethings i get the url duplicated like this www.mywebsite.com/#googtrans(en|es)#googtrans(en|es)

This is my list, when I click any of the links will trigger the function

 <ul class="dropdown-menu dropdown-menu-left dropdown-language">
 <li><a class="english" onclick="changeLanguage('en');" >English</a></li>
 <li><a class="spanish" onclick="changeLanguage('es');">Spanish</a></li>
 <li><a class="chinesse" onclick="changeLanguage('en|zh-CN')">Chinesse</a></li>
 </ul>

<script type="text/javascript">

function changeLanguage(language) {

    window.location.search += "#googtrans(en|" + language +")";
};

1
  • I'm just curious, but can't you use a link href instead of a function? Commented Nov 12, 2016 at 1:41

3 Answers 3

1

You are using a wrong expression. Try to use instead:

function changeLanguage(language) {
    window.location = "#googtrans(en|" + language +")";
};

If you want the page to reload you have to delete the hash # and replace the code with: window.location += "/googtrans(en|" + language +")";

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

5 Comments

Beat me :) But that's it.
it works but i would like it also to refresh the page with that url
@GilbertoQuintero If you want the page to reload, you have to delete the hash sign #, because it prevents the page from reloading.
@K.Daniek if i delete the # it tthrows me Server Error in '/' Application.
@GilbertoQuintero Because now you are changing the path. I've edited my post, check it. But remember that you have to have a separate file with translated content.
0

Another alternative is this:

function changeLanguage(language) {
    // get the origin of the url and define the language
    var host = window.location.origin;
    var language = 'french';

    //make sure the language is defined before this line
    window.location.replace(host + "#googtrans(en|" + language );
};

Comments

0

'#' characters don't belong in the search field of a URL, because '#' is the start character of the URL's optional fragment identifier. The '#' start character and fragment identifier together make up the contents of window.location.hash

But browsers differ in how they respond to attempts to insert a hash character in window.location.search. IE 11 throws an error saying the URL is invalid. Firefox silently converts the '#' to percentage encoding '%23'. Yet another browser may happily put the '#' character in the search field but treat it as the start of the "fragment" field when assembling the whole URL from the window.location object.

Note browsers are supposed to reload the page from the server when the search field changes, discarding cached copies in local or intermediary storage between the client and server.

On the other hand changes to the hash fragment alone do not trigger a page refresh and are used for internal document navigation. Browsers do not send contents of the hash fragment to the server when making a page request, but may use the fragment to scroll through a document returned.

Without knowing what the server code is expecting it is hard to say how to fix the problem properly. Typically, however, text string values are encoded using encodeURIComponent on the client side, and unencoded on the server side.

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.