The purpose of this exercise is to demonstrate how URL query strings can be used to create search widgets that can be embedded in websites.
This lesson continues from Part A, which covered the basics of URL query strings and how they can be used to construct searches. Now we will use these concepts to create a search plug-in for our users so that they can easily find Creative Commons images on Flickr.
In the following project, we will create a simple search box where the user can type in their own search term and then will be taken to a list of results on Flickr.
The basic pieces are simple enough, and once we put them all together we will have created a nice little search plug-in.
For this phase of our project we are going to use a great Open Source application for this project- JSBin. JSBin is configured in a way that is in keeping with real-world web development. Let's look at some of the specifics of JSBin:
We will be using JSBin with some code snippets below- if your screen size is too small to see the embedded JSBin exercises, click on the JSBin logo (Dave, the Bin-bot, who looks like a little can) to go straight to the full version. We will also be using JSBin for our project- the link at the bottom of the page will take you to the JSBin site to complete it.
We're all familiar with search boxes, especially this one:
But have you ever wondered how to create your own? Well, with query strings and HTML forms,
we can do this fairly easily. The code is actually pretty straight-forward:
we'll grab the user's input and save that in our queryString variable which
we'll then manipulate into our URL. If this isn't making any sense- don't panic! Read on...
We aren't going to get into too much detail for this exercise, but for you to fully understand what we're talking about, we'll need to discuss a couple of things:
element- these
are written out using opening and closing tags, enclosed in angle brackets, like so:
<h3>This is a heading</h3>
The <h3> element is a third level heading. The heading
content begins after the <h3> tag, and ends with the closing tag,
which has a backslash, like so: </h3>.elements like so:
<p>This is a paragraph with a <a>link</a> inside it</p>
<p> tag is for a paragraph and the
<a> tag is for a link.]variables are similar to those used in Algebra- except
that we can use variables to represent any type of data,
not just numbers. A variable can be thought of as a
shorthand- instead of having to type out "schipperke" a bunch of times and risk
misspelling things, we can store the text inside of a
variable, such as term
like so:
var term = "schipperke";
var to signify that we are about to define a
variable. Also, when we assign a
value that is not a number, it needs to be inside quotation marks.]
"Let's search for " + term + " photos in Flickr."The output would be:
Let's search for schipperke photos in Flickr.
The real beauty of variables is that we can change
them later, like so:
term = term + " puppy";Our
variable term is now equal to:
"schipperke puppy" and the output of our script above would now be:
Let's search for schipperke puppy photos in Flickr.
selectors. Using jQuery to do this can be very complicated
with most real-world webpages and I highly recommend taking the jQuery tutorial at Code
School (try.jquery.com) if you want to learn more, but the basic idea is this:
$('a').text("Schipperke");
<a>
element on the page. We accomplish this by using the a
selector.p
selector to write:
$('p').text('This is the new paragraph text.');
$('a').text(term);
id selector. We can
select an element based on its id like so:
$('#textInput').text("We are targeting just the element with the textInput id.");
The symbol for selecting the id is the #. Therefore, the script
above is targeting the element with id="textInput" and is then grabbing the value of
that input. Simply put: that script is grabbing the text the user types into the
search box.For the purposes of this exercise, we'll have to talk a bit about HTML forms, but I'm going to keep it simple and stick to only those pieces we'll be using.
text input which looks like this:
<input type='text' name='searchTerm' id='searchTerm'>Notice that we have some
attributes here- the type
attribute specifies that we are asking for text and the
name and id attributes
are used to manipulate the element. In our script, we'll use the id
to specifiy where we will grab the input value. The
value of a text input field is what the user types in. We can insert a
value by specifying that attribute and value
pair if we want to have a suggestion already loaded in the field, but for now, we'll just leave it blank.
For an example of a pre-loaded value in a search form, look back
at the example in Part A (the value
for that form input is "URL Query String Exercise").
submit. Once the user has entered her search terms into the text box,
she will need a way to signal that she is ready to search for those terms. By clicking
on the submit button, our script will be run and her search will be under way.
Now it is time to work on our project and finally get our hands dirty! You will be creating a search box that will allow your viewers to search for Creative Commons photos in Flickr right from your site. To do this, you will:
Got it? Let's go! Click the button below to open the project.
View the project