简体   繁体   中英

How do I pass a variable to a javascript file <script src=“../js/myfilename.js”></script>

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.

<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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM