Skip to content Skip to sidebar Skip to footer

Load A Different Javascript File According To Language

I'm trying to add a cookie banner to my website and it has two different languages: Italian and English. Language goes as http://www.grcparfum.it/home.php?section=letteradelpresid

Solution 1:

Problems you're probably encountering:

  • Some curly brackets are outside of the scope of PHP
  • It looks like you should be using == for the first if statement not !=.
  • You're trying to compare a constant called eng with $currentlang rather than "eng"

Try:

<?php$currentlang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING); // Saferif($currentlang == "eng"){ ?><scriptsrc="/js/cookiechoices-en.js"></script><?php } else { ?>{
        <scriptsrc="/js/cookiechoices-it.js"></script><?php }
?>

Solution 2:

I'm assuming it's never picking the eng option.

I think your bug is not putting eng in quotes:

if($currentlang !=eng){

Should be

if($currentlang != "eng"){

Solution 3:

Do this using JQuery:

url = 'http://www.grcparfum.it/home.php?section=letteradelpresidente&lang=eng';

functiongetParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = newRegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(url);
  // results = regex.exec(location.search); gets the url of current windowreturn results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var language = getParameterByName('lang');
english = '<script src="/js/cookiechoices-en.js"><\/script>';
italian = '<script src="/js/cookiechoices-it.js"><\/script>';
if(language == 'eng'){
    $('#script').html(english);
}elseif(language == 'ita'){
    $('#script').html(italian);
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="script"></div>

Post a Comment for "Load A Different Javascript File According To Language"