So what I am doing is making a special schedule. The best way I found to go about it to suit my needs is, each day on the calendar will have a name of a person, there is a limited list of names to choose from. Calendar setting is set to show only 1 day at a time which will be today. So the name of 1 person will only be visible as an event on the calendar. If I can search the whole page for the name, there will only be 1 of that name because calendar only shows 1 day at a time, then I will know using if statements who the person in-charge is on that day. -1 for output for all names except one name, that one name would be on the schedule.

On a website lets say http://www.w3schools.com/js/js_objects.asp for example, I would like to search for the word dog and if its there return the position, if not return a value such as -1...I know that there is a search function that searches a string for a word and if that string is found in a sentence it returns the position else returns -1 but how can I search a website's page, the example at the homepage searches the webpage for a word, but how can I make it search a URL such as the one above for a word and give me an output of -1 for no word found or any other number for a word found?




/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Robin Winslow :: http://www.robinwinslow.me.uk */

// Find and select all text in the document that matches the "value" of the element passed
// If no element if passed, it will attempt to use the value of "this"
// Only works in a browser that supports the "window.getSelection" method, as other selection methods don't support multiple selectionsz
function performMultiSearch(elem,searchElem) {
// set up variables
var searchString; // Will hold the text to search for
var theSelection; // Will hold the document's selection object
var textNodes; // Will hold all the text nodes in the document

// Set it to search the entire document if we haven't been given an element to search
if(!searchElem || typeof(searchElem) == 'undefined') searchElem = document.body;

// Get the string to search for
if(elem && elem.value) searchString = elem.value;
else if(this && this.value) searchString = this.value;

// Get all the text nodes in the document
textNodes = findTypeNodes(searchElem,3);

// Get the selection object
if(window.getSelection) theSelection = window.getSelection(); // firefox
else { // some other browser - doesn't support multiple selections at once
alert("sorry this searching method isn't supported by your browser");
return;
}

// Empty the selection
theSelection.removeAllRanges(); // We want to empty the selection regardless of whether we're selecting anything

if(searchString.length > 0) { // make sure the string isn't empty, or it'll crash.
// Search all text nodes
for(var i = 0; i < textNodes.length; i++) {
// Create a regular expression object to do the searching
var reSearch = new RegExp(searchString,'gmi'); // Set it to 'g' - global (finds all instances), 'm' - multiline (searches more than one line), 'i' - case insensitive
var stringToSearch = textNodes[i].textContent;
while(reSearch(stringToSearch)) { // While there are occurrences of the searchString
// Add the new selection range
var thisRange = document.createRange();
thisRange.setStart(textNodes[i],reSearch.lastIndex - searchString.length); // Start node and index of the selection range
thisRange.setEnd(textNodes[i],reSearch.lastIndex); // End node and index of the selection
theSelection.addRange(thisRange); // Add the node to the document's current selection
}
}
}

return;
}

// Will find and select the first instance of the value of the passed element, then when called again will moves on to the next instance
// If no element passed it will try to get the value from "this"
function performSingleSearch(elem,searchElem) {
// set up variables
var searchString; // Will hold the text to search for
var theSelection; // Will hold the document's selection object
var textNodes; // Will hold all the text nodes in the document

// Set it to search the entire document if we haven't been given an element to search
if(!searchElem || typeof(searchElem) == 'undefined') searchElem = document.body;

// Get the string to search for
if(elem && elem.value) searchString = elem.value;
else if(this && this.value) searchString = this.value;

if(searchString && searchString.length > 0) { // make sure the string isn't empty, or it'll crash.
if(window.getSelection) { // Firefox
// Get the selection
theSelection = window.getSelection();

// Get all the text nodes in the document
textNodes = findTypeNodes(searchElem,3);

// If there's already a selection, and it's the string we're searching for
var searchMatch = new RegExp(searchString,'i');
if(theSelection.rangeCount == 1 && searchMatch(theSelection.getRangeAt(0).toString())) {
var currentRange = theSelection.getRangeAt(0);
theSelection.removeAllRanges();

var newRange = null;

// Move on to the next occurrence of it by iterating through text nodes...:
for(var i = 0; i < textNodes.length; i++) {
// If this text node is before the currentRange, ignore it and carry on to the next one
if(currentRange.comparePoint(textNodes[i],0) == -1 && currentRange.startContainer != textNodes[i]) continue;
// If this text node is the same as the currentRange, find the point in the currentRange
else if((currentRange.comparePoint(textNodes[i],0) == -1 && currentRange.startContainer == textNodes[i]) || (currentRange.comparePoint(textNodes[i],0) == 0)) {
// Create a regular expression object to do the searching
var reSearch = new RegExp(searchString,'gmi'); // Set it to 'g' - global (finds all instances), 'm' - multiline (searches more than one line), 'i' - case insensitive
var stringToSearch = textNodes[i].textContent;
while(reSearch(stringToSearch)) { // While there are occurrences of the searchString
// Test if the index is after the currentRange's position
if(reSearch.lastIndex - searchString.length > currentRange.startOffset) {
// This is the new search position - empty the old selection and add the new selection range
theSelection.removeAllRanges();
newRange = document.createRange();
newRange.setStart(textNodes[i],reSearch.lastIndex - searchString.length); // Start node and index of the selection range
newRange.setEnd(textNodes[i],reSearch.lastIndex); // End node and index of the selection



Please see the homepage example if you dont know what I am talking about. Its too long to post here.