0

I'm trying to pass a lang variable in my js files.

Actually I have this:

<script src="assets/js/plugins.js?lang=en"></script>

In plugins.js, I have this:

var LIBS = {
    // Chart libraries
    validation: [
        "assets/libs/validation/jquery.validation.min.js",
        "assets/libs/validation/lang/xxxxxx.min.js"
    ],
    ...
};

How can I get the lang value passed to replace my xxxxxx by the lang en ?

Thanks.

  • 1
    Possible duplicate of http://stackoverflow.com/questions/1017424/pass-vars-to-javascript-via-the-src-attribute – H77 Jun 24 '16 at 13:27
  • Yes I have php running. –  Jun 24 '16 at 13:30
  • Either do this server side, or make your variable available globally, so that it is accessible to other scripts included on your HTML page. This would be the normal way of doing it. Server side scripting is not really necessary. – ManoDestra Jun 24 '16 at 13:30

3 Answers3

0

try this in your plugin.js

var LIBS = {
    // Chart libraries
    validation: [
        "assets/libs/validation/jquery.validation.min.js",
        "assets/libs/validation/lang/xxxxxx.min.js"
    ]
};
LIBS.tmpValidation = LIBS.validation;
LIBS.validation = [];
LIBS.lang = "en";
Object.defineProperty(LIBS, "validation", {
  get : function(){
    var self = this;
    return this.tmpValidation.map(function(item){
      return item.replace(/xxxxxx/g, self.lang)
    })
  }
});
console.log(LIBS.validation);

You can now define your plugin in such a way that lang property is set to LIBS before invoking LIBS.validation

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
-1

try

var s = document.getElementsByTagName('script'),
    ss = s[s.length-1];
    lang = ss.src.match(/\?lang=(\w\w)/);
// sure...  check it exists!
console.debug(lang[1]);

inside plugins.js

fedeghe
  • 1,243
  • 13
  • 22
  • why this downvote? this is the only real valid answer in this page to the question. Just spite, shame on these downvoters, you're right .. you can't challenge me and the only thing you can do is just downvote and run away without arguing cause you have no clue. You could be much better than that. – fedeghe Jul 03 '16 at 20:35
-1

You can use a global variable.

Global variable:

I've used an object here to store settings, you could add more settings, if you wish

var settings = { lang:'en' };

Your other script (plugins.js):

var LIBS = {
    // Chart libraries
    validation: [
        'assets/libs/validation/jquery.validation.min.js',
        'assets/libs/validation/lang/' + settings.lang + '.min.js'
    ],
    ...
};

Here's a full example script that does precisely what I'm talking about above. I've got LIBS declared in the same script, but it could just as easily be included in plugins.js and declared below the setting of the global settings variable in this script instead...

Example Script:

<script>
    var settings = { lang:'en' };
    console.log(settings); // Show that the value has been set.

    var LIBS = {
        // Chart libraries
        validation: [
            'assets/libs/validation/jquery.validation.min.js',
            'assets/libs/validation/lang/' + settings.lang + '.min.js'
        ]
    };

    // Show that LIBS has been set with the language setting from the global variable.
    console.log(LIBS);
</script>

After you've run this, then you will see that the variable has been picked up and the second entry in the array is assets/libs/validation/lang/en.min.js

Or to be closer to your own example...

<script>
    var settings = { lang:'en' };
    console.log(settings); // Show that the value has been set.
</script>
<!-- Following line must come after the script above
     this JS file can now access that global settings variable -->
<script src="plugins.js"></script>
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • It doesn't work except if I move the call to `setting.lang` outside the `LIBS` –  Jun 24 '16 at 13:36
  • The global variable must be declared above your plugins.js, of course, for it to be picked up. – ManoDestra Jun 24 '16 at 13:36
  • Just tested it myself and it works. I'll update my answer with my example code. – ManoDestra Jun 24 '16 at 13:40
  • This answer has nothing to do with the question – fedeghe Jun 24 '16 at 14:05
  • Making a variable value available to another JavaScript file for use. My answer is ENTIRELY related to the requirements of the OP and solves that problem very simply in the correct fashion, in accordance with development standards. But if you feel I've missed an aspect of the requirements and failed to solve the problem somehow, then do advise me of it and I'll cater for it in my (accepted) answer. Sour grapes much? The OP doesn't require to do it the way he was doing it. His requirement is to pass a value to another script. And this is how it's done. – ManoDestra Jun 24 '16 at 14:34
  • sorry @ManoDestra but "in accordance with development standards" is really just only Your opinion (luckily)... my answer (the one you downvoted just for spite) not only solves the problem exactly how requested but even uses a lot less code and does not require to wire the value in ad additional script tag. Unluckily the fact that the OP has accepted it is enough for You to believe you did it right. – fedeghe Jun 24 '16 at 15:10
  • The OP's requirements were met with the correct and simplest solution, rather than trying to patch the incorrect and overly complex attempt at a solution. He's trying to pass a parameter to a script in order to initialize a value in an array. He can do this very simply with an object at the global level accessed in the script itself without trying to pass bizarre querystring-esque parameters. Very simple JavaScript, no need for the querystring parameters at all. Problem solved :) – ManoDestra Jun 24 '16 at 15:56
  • You can smile and argue your own solution as much as you like but it is NOT the one that solves the problem as has been formulated by the OP. Maybe you should read it a second time. Now I know why a regex like `/\?lang=(\w\w)/` can be painful for you – fedeghe Jul 03 '16 at 20:59
  • Your solution is an over-engineered one. It is the appropriate solution. It is a Band-Aid placed over a poor design choice. The correct, concise and simplest solution has been given. Your sour grapes and continued trolling doesn't change that fact. Good day to you :D – ManoDestra Jul 04 '16 at 20:11