Tuesday, April 16, 2024

HTML Goodies: Script Tip: Week 43

 

Tip Forty-Tree…

     I have found that one of the hardest things about writing a JavaScript is trying to set up code so that every possible thing a user could do is handled in one way or another. I always forget something.

     The author of this script put in some code that I would never have thought of. Many of you know that when using a search engine, a plus sign (+) between words is a good idea to help to search. Today, we look at the code that searches the keywords entered in this script. If a space is found, then a plus sign replaces it.

     But first, refresh yourself on the effect and code of this Script.


Enter the Keyword :

Select the Search Engine(s):
Yahoo
Altavista
WebCrawler
Excite
Lycos


Here's the Code

(It's big. Get it all.)


     I am worried about the code that makes up the first function, wordsplit(items). It looks like this:

function wordsplit(items)
{
var charect = "";
for (var n = 1 ; n <= items.length ; n++)
{
if (items.substring(n-1,n) == " ")
{ charect+="+"; }
else
{ charect+=items.substring(n-1,n); }
}
return charect;
}

     Now, this is not the function that performs the search. That function is called search(), and it's coming later. This function is one that will be called upon as part of search. Rather than going back and forth, I'll tell you what this does so that you can just plug it in later.

     Notice the function has the parameter "items" in the instance. That means that "items" will be returned to the function to be acted upon.

     The function begins with a variable, "charect", being set and assigned the value of nothing. See the empty quotes? It means "no space".

     Next a loop is created with this:

for (var n = 1 ; n <= items.length ; n++)

     The loop's format follows this pattern: A variable, "n", is created and given the value of 1. Then a condition is set. The condition states that "n" is less than, or equal to, the length of characters in the item. Finally, the third element of the loops states what should happen as long as the condition is not met. The code "n++" means that "n" should move upward incrementally. Double negative signs would mean moving downward in the same fashion.

     So what in the world does this do? It looks at each character in the item one by one. Again, this is a little out of order. The "item" will be given the same value as what the user writes into the text box. Stay with me, that will happen soon. For now, just take my word that that's what it means.

     Each time a new character is looked at, an If/Else statement is employed. If the character is a space, then that space is replaced with a plus sign (+). If it is not a space (else), the character stays the same and the loop fires up again, checking the next character.

     This happens as long as "n" is less than the length of characters in the text entered by the users. Every character is checked, every empty space is given the new value of (+).

     Then, after all characters have been checked, the loop shuts down and the new text is returned. That means the script now has it stored for later use. And we will use it later.

Next Week: Do The Search


Learn to write your own JavaScripts with the

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured