Pass jquery variable in filename - as if it were get

Asked

Viewed 367 times

2

Holá, guys, I’m not getting information about this because I don’t know how to look, I tried to get variable jquery, and things like that and didn’t return what I need.

It is as follows: I need to pass a variable in js in the name of the file and inside this file to get this variable:

Ex:

<script src='app.translator.js?lang=pt_BR'></script>   

inside the file 'app.translator.js' as I get this variable 'lang'??

var lang = ????????;

============edited from here down with an answer=========================

I want to leave here another solution that I got

<script src='app.translator.js'></script>   
//conteudo do arquivo
var appTranslate = function(){
    var formLogin = function(lang){
        //minha funcao com a lang carregada aqui...
    };
    return {
        init: function(lang){
            alert(lang);
            // passo a lang para minha funcao
            formLogin(lang);
        },
    };
}();

after loading the app.translator.js on the html page footer, I just called the function

<script src='jquery.js'></script>
<script src='app.translator.js'></script>
<script>
$(function(){
// vai mostrar o alert('pt_BR')
appTranslate.init('pt_BR');
});
</script>

2 answers

4


var el = document.getElementById('lang'),
    param = el.getAttribute('src').match(/lang=([^]*)/)[1];
alert(param);
<script id='lang' src='app.translator.js?lang=pt_BR'></script>

  • I want to make it clear that this answer served exactly what I wanted. however I found another solution and edited the question with the answer. Thank you all.

1

Opa inside Translator.js you can capture the value by using jquery thus:

Declare your script like this:

<script src='app.translator.js' lang='pt_BR' parametro='teste'></script>  
var variaveis = $('script[src*=app.translator.js]');

having the element can be so capped (inside the Translator.js):

alert(variaveis.attr('lang'));
  • +1 Opa, I liked the solution.

  • @War It didn’t work for me here, you can make a functional example on https:/jsfiddle.net?

  • 1

    @Sergio, man, I didn’t even realize to pass the variable this way, I did it here and it worked. appTranslator.init('pt_BR'); inside my app.translator.js looks like this: var appTranslate = Function(){ Return { init: Function(lang){ Alert(lang); }, }; }();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.