1

How do I pass a variable to a javascript file?

Here is the call to my js file that I have on my page:

<script src="../js/myfilename.js"></script>

Here is the value that I want to pass into the javascript file:

language_value_xxx = "uk";

Here is the trimmed down javascript file (myfilename.js):

(function (language_value_xxx) {
....
language: language_value_xxx,
....
1
  • Read about the "module pattern", "Common modules" and "RequireJS". Or just share those variables in the global scope under a unique namespace. Commented Dec 13, 2013 at 0:14

1 Answer 1

3

1.

<script> language_value_xxx = "uk"; </script>
<script src="../js/myfilename.js"></script>

2.

<script data-lang='uk' src="../js/myfilename.js"></script>

// filename.js
var lang = []
    .slice
    .call(document.querySelectorAll('script'))
    .pop()
    .dataset
    .lang
    ;

Anyway, this methods are not a good practice at all )

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, although this may not be the best approach, it will have to do untill I find a better way. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.