Accessing script src CGI params in Javascript
This has come up a few times now as a way to avoid server side coding and use Javascript more dynamically based on the CGI parameters passed in the src attribute of the <script> tag. This script has been cross-browser tested in Chrome5+/Safari4+/FF3.6+/IE6+.
The way this script works is:
- create/increment global index of this script in the DOM
- find all script tags currently in the DOM
- count occurences of this script using the sentinel __JSCGI=1
- assign scriptElement variable to the current instance of the script
- parse CGI parameters from src attribute
Being setup this way also allows the script to be included in the DOM multiple times specifying different CGI parameters to determine different functionality for the script.
A simple demo can be found here using the cgi.js file here.
If you have a better way of doing this please do share.