Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search engine coming up with random results 1

Status
Not open for further replies.

skills

Programmer
Jun 24, 2003
60
US
I have designed an internal search engine. I have it working to the point that it will search and find what I am looking for. My problem is that it is inconsistent, and when running the search engine with the same search string multiple times, it finds a different number of results every time.

Below is the code:

<code>

if (cs2 == true)
{
index_page = &quot;cs2.htm&quot;;
search(index_page,input)
}
if (cssc == true)
{
index_page = &quot;cssc.htm&quot;;
search(index_page,input)
}
if (eso == true)
{
index_page = &quot;eso.htm&quot;;
search(index_page,input)
}
...


function search(first,find)
{
var i = 0;
var first_window = window.open(first,&quot;first_window&quot;,&quot;width=1,height=1,top=1700,left=1700&quot;);
while (i < first_window.document.links.length)
{
var second_window = window.open(first_window.document.links,&quot;second_window&quot;,&quot;width=1,height=1,top=1700,left=1700&quot;);
var j = 0;
while (j < second_window.document.links.length)
{
var pdf_link = &quot;&quot;;
pdf_link = second_window.document.links[j];
var pdf_link_text = &quot;&quot;;
pdf_link_text = second_window.document.links[j].innerHTML;
compare(pdf_link,pdf_link_text,find);
j++;
}
second_window.close();
i++;
}
first_window.close();
}

function compare(link,text,desired)
{
var found = text.toUpperCase().indexOf(desired.toUpperCase());
if (found != -1)
{
descriptions[array_index] = text;
listoflinks = listoflinks + link + &quot;,&quot;;
array_index++;
}
}


<code>

What I have it do is read in a search string from a form, as well as where to search in. That is the first part of the code. It sends the main page to the search function. Inside the main page, it has links to sub pages. Search opens up each sub page and looks at the links. It then sends it to compare, which puts it on the arrays for displaying at the end if it matches what I am looking for. I have gotten it to find everything before, so I know it does work. Why is it random though?

 
I have changed the code a bit. Here is the changes:

<code>

if (cs2 == true)
{
searchedin = searchedin + &quot;CS2, &quot;;
index_page = &quot;drawcd/cs2_x.htm&quot;;
search(index_page,input)
index_page = &quot;drawcd/cs2_1.htm&quot;;
search(index_page,input)
index_page = &quot;drawcd/cs2_2.htm&quot;;
search(index_page,input)
index_page = &quot;drawcd/cs2_3.htm&quot;;
search(index_page,input)
index_page = &quot;drawcd/cs2_4.htm&quot;;
search(index_page,input)
index_page = &quot;drawcd/cs2_5.htm&quot;;
search(index_page,input)
}
if (cssc == true)
{
searchedin = searchedin + &quot;CSSC, &quot;;
index_page = &quot;drawcd/cssc_x.htm&quot;;
search(index_page,input)
index_page = &quot;drawcd/cssc_5.htm&quot;;
search(index_page,input)
}
...

and then

function search(first,find)
{
var i = 0;
var first_window = window.open(first,&quot;first_window&quot;,&quot;width=1,height=1,top=1700,left=1700&quot;);
while (i < first_window.document.links.length)
{
var pdf_link = &quot;&quot;;
pdf_link = first_window.document.links;
var pdf_link_text = &quot;&quot;;
pdf_link_text = first_window.document.links.innerHTML;
compare(pdf_link,pdf_link_text,find);
i++;
}
first_window.close();
}

<code>

I am still coming up with eratic results.
Any suggestions would be great.

Thanks,

Jonathan
 
it's hard to say with just the partial code above, but i can foresee problems with search() because first_window's document is not guaranteed to be loaded before you attempt to iterate its links.


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
How do I make sure that the document is loaded before I check it's links?

Jonathan
 
in IE only, you can check the readyState property of the document object. readyState will be &quot;complete&quot; when the page has loaded.

something like this should work:
function search(first,find) {
var i = 0;
var first_window = window.open(first,&quot;first_window&quot;,&quot;width=1,height=1,top=1700,left=1700&quot;);

doSearch(first_window);

first_window.close();
}

function doSearch(oWindow) {
// save oWindow in case we have to call doSearch again
if (oWindow) self.oWindow = oWindow;
oWindow = self.oWindow;

if (oWindow.readyState == &quot;complete&quot;) {
while (i < oWindow.document.links.length) {
var pdf_link = oWindow.document.links;
var pdf_link_text = pdf_link.innerHTML;
compare(pdf_link,pdf_link_text,find);
i++;
}
}
else {
setTimeout(&quot;doSearch();&quot;, 50);
}
}


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Jeff,

You were right about the page loading. That solved that problem. The code above is a bit excessive, and I was able to reduce it to this:

var first_window = window.open(first,&quot;first_window&quot;,&quot;width=1,height=1,top=1700,left=1700&quot;);
while (first_window.document.readyState != &quot;complete&quot;)
{
var waiting = 0;
}
while (i < first_window.document.links.length)
{
var pdf_link = &quot;&quot;;
pdf_link = first_window.document.links;
...

So, this works, and is great. All your help is much appreciated. I do have one more question, related to this program. Since it is a different topic, I made a new string called &quot;anchor array help&quot;. Please take a look at it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top