0

I would like to change a part of the url inside of the src attribute. This would be called with a click function. This could change the language of the like button without refreshing the page and when another language was selected previously.

This works well, but since there are multiple languages, it only works once; when the base "state" is hu_HU (Hungarian):

$('.fb-like').attr('src', function(i, src) {
return src.replace("hu_HU","ro_RO"); 
});

I changed the code to accomplish the goal according to the solution in this question: JQuery: how to replace all between certain characters? but without any success:

$('.fb-like').attr('src', function(i, src) {
return src.replace(/locale\=.+?\&amp/g,"locale="+ro_RO+"&amp");
});

What am I missing? Any help would be appreciated! Thank you in advance!

Here is the fiddle: http://jsfiddle.net/iorgu/a0aptp3g/2/

Community
  • 1
  • 1
iorgv
  • 787
  • 2
  • 11
  • 26

1 Answers1

2

This is related to one of my earlier answers.

Now, you intend to change the source of an iframe, so we can no longer assume it is window.location.href. So, I believe a modified version of the function should solve your issue:

function setGetParameter(paramName, paramValue, url)
{
    var url = !!url ? url : window.location.href;
    if (url.indexOf(paramName + "=") >= 0)
    {
        var prefix = url.substring(0, url.indexOf(paramName));
        var suffix = url.substring(url.indexOf(paramName));
        suffix = suffix.substring(suffix.indexOf("=") + 1);
        suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
        url = prefix + paramName + "=" + paramValue + suffix;
    }
    else
    {
    if (url.indexOf("?") < 0)
        url += "?" + paramName + "=" + paramValue;
    else
        url += "&" + paramName + "=" + paramValue;
    }
    return url;
}

And you call it like this:

$('.fb-like').attr('src', setGetParameter("locale", "ro_RO", $('.fb-like').attr('src')));

The result is that the love guru has successfully changed the locale.

Fiddle.

Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • I figured out that the Comment Plugin can be also replaced dynamically with addressing it with a class that is given inside the DOM, this is the `.fb_ltr`, your code works here as well. (within the basic code that FB gives to embed) – iorgv Jun 26 '15 at 06:41