javascripthtmlurlvariablessrc

Pass vars to JavaScript via the SRC attribute


In my HTML file I have linked to the JS with:

src="myscript.js?config=true"

Can my JS directly read the value of this var like this?

alert (config);

This does not work, and the FireFox Error Console says "config is not defined". How do I read the vars passed via the src attribute in the JS file? Is it this simple?


Solution

  • <script>
    var config=true;
    </script>
    <script src="myscript.js"></script>
    

    You can't pass variables to JS the way you tried. SCRIPT tag does not create a Window object (which has a query string), and it is not server side code.