Pass variable jquery to filename - as if it were get

2

Hello, guys, I'm not getting information on this because I do not know how to look, I tried to get jquery variables, and stuff like that and did not return what I need.

It is as follows: I need to pass a variable into js in the file name and within that file get this variable:

Ex:

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

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

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

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

I want to leave another solution here that I have

<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 app.translator.js into 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>
    
asked by anonymous 20.05.2015 / 17:10

2 answers

2

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>
    
20.05.2015 / 17:44
1

Oops inside the translator.js you can capture the value using jquery like this:

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 captured like this (inside the translator.js):

alert(variaveis.attr('lang'));
    
20.05.2015 / 17:17