Computer Help Community

Community

Cyber Tech Help Community

New Tutorials

PC Tutorials

New Downloads

More Downloads
PC Downloads

Member Testimonials

Open Member Quote   Am grateful that I found this site. Receiveid prompt and helpful advice shortly after posting my first "insolvable problem". - Popster  Close Member Quote
Member Testimonials
MY CYBER TECH HELP

LATEST TOPICS

Tech Help Community

Free Antivirus Scan

Free Virus Scan and a listing of the top 10 viruses in the wild - Free Antivirus Scan
Free Online Antivirus Scan

File Extension Database

Find what program a filetype belongs to in our searchable File Extension Database.
File Extension Database

Related Microsoft Links

Services
Cyber Tech Help Community

To the top of the page to top

 



Tutorials | URL Parameter Passing in Javascript

Publish date: 19:02 Thursday, 28th July 2005
Written by: Steven Bentley
Audience intended for: Web Designers
Category: Web Design

Go up a levelGo up a level

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:


urlquery=location.href.split("?")rnurlterms=urlquery[1].split(",")

These two lines of code will return an array called urlterms which is derived from the URL, so if you went to

Code:


filename.htm?23,45,67,54

then you'd find that

Code:


urlterms[0]==23
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:


urlquery=location.href.split("#")

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:


if(urlquery[1])

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.

 

Please Note: if you have any questions about this tutorial please ask on our support forums
If you have written a tutorial of your own and would like to have it here on Cyber Tech Help all you have to do is Submit your tutorial and it will be reviewed by the Administrator.