Pass variable to external JS file?

前端 未结 4 1781
遇见更好的自我
遇见更好的自我 2020-12-19 04:28

Is it possible to pass a variable to a linked .js file? I tried this:



        
相关标签:
4条回答
  • 2020-12-19 04:46

    No, you can't pass parameters like that and have the script read them in.

    Technically you could grab them from the <script> tag, but that would be a real mess.

    Could you just output a script block before you include the file?

    <script type="text/javascript"> var timeout = 1000; </script>
    
    0 讨论(0)
  • 2020-12-19 04:47
    <script type="text/javascript">
    var imagesPath = "emblematiq/img/";
    </script>
    <script type="text/javascript" src="emblematiq/niceforms.js"></script>
    

    This will work fine on server

    0 讨论(0)
  • 2020-12-19 04:52

    try this:

    <script>
    var myvariable = "foo";
    </script>
    <script src="/link/to/js.js"></script>
    
    0 讨论(0)
  • 2020-12-19 05:03

    No, but you can pass a the value directly to a function in that file or set a variable value that will be used in the external file.

    0 讨论(0)
提交回复
热议问题