You've probably seen sites which are programmed with a server-side language like PHP or a CGI script which use the URL to pass parameters around.
For example if you look at the URL of a Google search results page, it looks like this: http://www.google.com/search?q=javascript - the bit after the q= is the query that I entered, in this case JavaScript, and I can change that to anything I like without having to go through their search box.
If you're working in JavaScript there are a couple of reasons why you might want to be able to do this, firstly if the pages of your site are being dynamically generated based on user input in a form then you can make a static link from another part of your site or from another site, for example if you're using a JavaScript search system you can link to the search results for a particular term. Another use is during development, often I make the "behind the scenes" stuff before I put the interface in, so it's handy for testing.
This week's tip lets you accept parameters from the URL using JavaScript
Code:
These two lines of code will return an array called urlterms which is derived from the URL, so if you went to
Code:
then you'd find that
Code:
urlterms[0]==45
urlterms[0]==67
urlterms[0]==54
You can also put text in, although any spaces you've left will come through as %20
You can have two copies of this code so that you can call two different functions from the URL.
Code:
will also work, although it's seems a little unreliable in Netscape. Just pass the stuff from the array in this case into a different function.
You can use
Code:
to check that there is a value after the ? or # if you find that you're getting undefined or JavaScript errors or any other unexpected results.
