I am wondering whether is it possible to pass any variable to external java-script file
For example
I have this
tl;dr, Yes, but you need to use a server-side langauge
This seems like a good time to explain the fineries of web server-client interaction. This is the most basic explanation:
When a client requests a file (A page, a picture or script) the server looks for it in the appropriate folder. If it isn't found it sends a familiar 404 code back, or if it is found, it sends the contents of that file back (wrapped in http headers of course).
When it comes to dynamic pages (like .php
files) the web server has to act slightly differently. Before sending them to the client, the server runs the file through a program called a parser. It then sends the output of the parser back to the client (The parser can also modify headers). The request variables (The bit after the '?' in the URL, or the variables sent by a POST request) are available to the dynamic script in some manner. In PHP they're available using the $_GET/POST globals. These variables are parsed by the server, not the client, so you can have them anywhere.
Now, specifically to your problem, when you use a tag, the client asks for the file specified by the
href
attribute. This doesn't have to be a .js
file, that's just convention. The type
attribute tells the client what type of file it is. If you write PHP (or other) code to generate Javascript (even if its just include 'file1.js'
) you can use PHP to grab the sent GET/POST variables. Then the Javascript sent is executed by the client.
Sorry if this covers stuff you already know, I'm just trying to be complete