SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 43

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

 

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

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.