While browsing a website, say http://www.example.com, it is possible that the user enters some subdomain say http://www.sub.example.com
But the base webdomain remains the same.
In my firefox extension, I need to develop a “session” which remains active until a user remains on the same domain.
A simple solution would be to extract the domain from the url first. Then split the string with “.” as token. Thus, we get sub and example for the second case and just example for the second case. I can then compare both. If any are equal, I deduce that the domains are same.
Though this might work, this seems more like a hack and might be prone to false positives/negatives.
Is there a more efficient / cleaner solution to the same problem?
Info: I am using GWT to build the extension
You should use nsIEffectiveTLDService, it will handle things like
sub.example.co.ukcorrectly. You can pass annsIURIinstance tonsIEffectiveTLDService.getBaseDomain(),aAdditionalPartsparameter should be zero. Forhttp://sub.example.co.uk/fooyou will getexample.co.ukback – the actual “domain” part. Then you can simply compare the domain names for two URLs.