Is it possible to pass GET or POST variable to external javascript

后端 未结 4 1078
广开言路
广开言路 2021-01-26 08:08

I am wondering whether is it possible to pass any variable to external java-script file

For example

I have this

4条回答
  •  清歌不尽
    2021-01-26 08:25

    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

提交回复
热议问题