Pass vars to JavaScript via the SRC attribute

Robin Rodricks picture Robin Rodricks · Jun 19, 2009 · Viewed 14.1k times · Source

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?

Answer

Itay Moav -Malimovka picture Itay Moav -Malimovka · Jun 19, 2009
<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.