Javascript Tutorial Example
Example
Javascript Array
JavaScript Boolean
JavaScript Date
JavaScript Math
JavaScript Number
JavaScript String
Javascript Tutorial
Javascript Introduction
Javascript Syntax
Javascript Enable
Javascript Location
Javascript External
Javascript Operators
Javascript Variables
Javascript Functions
Javascript Events
Javascript Statements
Javascript If
Javascript Else If
Javascript While
Javascript For Loop
Javascript Comments
Javascript Array
Javascript Alert
Javascript Confirm
Javascript Prompt
Javascript Print
Javascript Redirect
Javascript Pop Up
Javascript Date
Javascript Form
Javascript Void 0
JavaScript Comparison
JavaScript Switch
JavaScript Try Catch
JavaScript Special Text
JavaScript Throw
JavaScript Math
JavaScript RegExp
Javascript String
Javascript Strings
Javascript Length
Javascript Split
Javascript Search
Javascript Replace
Javascript IndexOf
Javascript Compare
Javascript Advanced
Javascript getElementById
Javascript innerHTML
JavaScript Browser
JavaScript Cookies
JavaScript Animation
JavaScript Image Maps
JavaScript Timing
JavaScript Create Objects
JavaScript Summary
IndexOf Ever want to find something in one of your JavaScript strings? Maybe even start your search halfway through the string? Well you're in luck! The string function indexOf lets you supply one required argument (the search query) and one optional argument (the offset) for all your simple searching needs.
If you are looking for a more powerful search feature, you should check out JavaScript's string search function, which has support for regular expressions, but not offsets.
As we mentioned above, indexof has two arguments, with the second one being optional:
1. SearchString - What you would like to search for.
2. Offset (optional) - How far into the string you would like the search to begin. If you want to search the whole string, omit this argument.
To start off with, we will be finding the position of the "www" in a typical URL string. The indexof function will return the location of the first match that it encounters.
<script type="text/javascript">
var aURL = "http://www.hiscript.com/";
var aPosition = aURL.indexOf("www");
document.write("The position of www = " + aPosition);
</script>
If you'd like to follow along with how this function does it's counting, here's a complete breakdown:
* h - 0 - no match
* t - 1 - no match
* t - 2 - no match
* p - 3 - no match
* : - 4 - no match
* / - 5 - no match
* / - 6 - no match
* w - 7 - maybe match
* w - 8 - maybe match
* w - 9 - It's a match, return the position of the start of the match (7).
Let's see what happens when we add another www to the URL string to see if this function gets confused or not.
If we use the indexof function to find the first "www", then we can use that position + 1 as a way to skip the first "www" and find the second.
<script type="text/javascript">
var aURL = "http://www.hiscript.com/www.html";
var aPosition = aURL.indexOf("www");
var secondPos = aURL.indexOf("www", aPosition + 1);
document.write("The position of www = " + aPosition);
document.write("<br />The position of the second www = " + secondPos);
</script>
By using an offset of 8 (7 + 1), we were able to skip the first "www" to instead find the second. In case you're interested in what the function was actually looking at after we gave an offset, we have it below:
* ww.hiscript.com/www.html
There are only 2 w's left at the beginning of the string, due to the offset, making it impossible for a match to be found at the first set of "www".

