I got a CGI/Perl script that reads databases and displays the results on template pages. I need to be able to get the name of the database that is being read from the url location to be able to use it in a link in the template page. The URL would look something like this:
in which I would like to get the [i] field from the URL. I heard an idea about creating a dummy <span> tag with an id, then within the span tag use document.write to write the location url into the span tag, then using the getElementById it might be possible to use substr(value1,value2) to identify the database call in the url. I just don't know how to do this if it works. Any ideas about what I could do to solve this problem?
There are many ways to get the query string values (name-value pairs) out of the URL (location) of the current page. Many examples use the String::split() method to split the location on the "?", the "&", and the "=". This keeps breaking the URL up into smaller arrays that can eventually be used to grab the query string name-value pairs. This is a decent solution, and may in fact be faster than my solution, but I find my own solution a bit more simple.
Once we have all these name-value query string pairs, we can either write them to the document as the document is still loading or, we can write them to DOM-accessed nodes after the document has finished loading:
When we run the HTML above at the given URL:
... this is the output of the resultant page:
Notice that the first 4 values were written as the document was loading (using document.write()). Then, once the document finished loading (just about), we grabbed a DOM node reference based on the ID, "database," and wrote the URL value to its innerHTML. These are two valid was of handling this situation.
Want to use code from this post? Check out the license.