[Project_owners] How to listen to location changes?

Stan James sjames at uni-osnabrueck.de
Sat Jan 29 10:52:28 EST 2005


Michael Keppler wrote:

> Hi all,
>
> I'm trying to listen to location changes of the browser and 
> implemented an nsIWebProgressListener. All seems to be fine (no syntax 
> errors etc.) except I don't get notified.
>
>
> var docService = Components.classes["@mozilla.org/docloaderservice;1"].
> getService(Components.interfaces.nsIWebProgress);
>
> docService.addProgressListener(wikipedia,15 | 128);
> //NOTIFY_STATE_ALL | NOTIFY_LOCATION
>
> ...
>
> onLocationChange: function(webProgress , request , location ) {
>     alert(location);
> },
>
> ...
>
>
> Does anybody see an obvious problem with the above code? Or should I 
> use a completely different approach to get notified on location changes?
>
> Ciao and thanks, Michael.
> _______________________________________________
> Project_owners mailing list
> Project_owners at mozdev.org
> http://mozdev.org/mailman/listinfo/project_owners
>
>
I'm no pro, but I am successfully detecting location changes with code 
that I found in the mozillazine forum:
http://forums.mozillazine.org/viewtopic.php?t=49716
-stan

const NOTIFY_STATE_DOCUMENT =
  Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT;
const STATE_IS_DOCUMENT =
  Components.interfaces.nsIWebProgressListener.STATE_IS_DOCUMENT;
const STATE_START =
  Components.interfaces.nsIWebProgressListener.STATE_START;

var myListener =
{
  onStateChange:function(aProgress,aRequest,aFlag,aStatus)
  {
    //dump("statechange"); 
    if(aFlag & (STATE_IS_DOCUMENT|STATE_START))
    {
      aRequest.QueryInterface(Components.interfaces.nsIChannel);
      gCurrentURL = aRequest.URI.spec; /* url is now in gCurrentURL */
    }   
  },
  onLocationChange:function(a,b,c){
        //dump("locationchange");
        if (c.spec != gCurrentURL)
        {
            gCurrentURL = c.spec;  /* url is now in gCurrentURL */
        }
  },
  onProgressChange:function(a,b,c,d,e,f){},
  onStatusChange:function(a,b,c,d){},
  onSecurityChange:function(a,b,c){},

  /*XXX
    This is not nsIWebProgressListenr method,
    just killing a error in tabbrowser.xml
    Maybe a bug.
  */
  onLinkIconAvailable:function(a){}
}

.
.
.

      window.getBrowser().addProgressListener(myListener , 
NOTIFY_STATE_DOCUMENT);


More information about the Project_owners mailing list