I am trying to read data from one frame's HTML before replacing it with a different frame using greasemonkey.

Here I demonstrate the problem: (see first 2 lines)

Code:
(function(){
 if (document.location.href.indexOf("header")!=-1)
  alert("poo");

 var b=window.location.href.indexOf("cache");
 var c=document.getElementsByTagName("script");
 for (var psf=0;psf<c.length;psf++){
  var p=c[psf].innerHTML.indexOf('updateStats(');
  if(p!=-1){
   GM_setValue("nst",c[psf].innerHTML.substring(p+12,c[psf].innerHTML.indexOf(')',p)),365);
   break;
  }
 }
 var a,f,n,l='';
 a=document.evaluate("//frame",document,null,XPathResult.ANY_TYPE,null);
 while(f=a.iterateNext()){
  n=f.name;
  if(n.length==8)
   document.location.replace(f.src);
 }
 do_stuff();
})();
This alerts NOTHING.

But if I add this single else statement:
Code:
(function(){
 if (document.location.href.indexOf("header")!=-1)
  alert("poo");
 else alert("crap"); //<------------- ADDED THIS LINE

 var b=window.location.href.indexOf("cache");
 var c=document.getElementsByTagName("script");
 for (var psf=0;psf<c.length;psf++){
  var p=c[psf].innerHTML.indexOf('updateStats(');
  if(p!=-1){
   GM_setValue("nst",c[psf].innerHTML.substring(p+12,c[psf].innerHTML.indexOf(')',p)),365);
   break;
  }
 }
 var a,f,n,l='';
 a=document.evaluate("//frame",document,null,XPathResult.ANY_TYPE,null);
 while(f=a.iterateNext()){
  n=f.name;
  if(n.length==8)
   document.location.replace(f.src);
 }
 do_stuff();
})();
It now alerts "poo" during the load, as well as a few times "crap".

I really don't know why it does this... but I want to replace those alerts with some real calculations based on substrings within the page. However none of them work in the same way that "poo" isn't alerted. I really don't want to alert things. If I replace that else with something trivial it will also fail.

Any clues on what is happening and how to avoid it?

Thanks!