UserJS.org

Avoiding page address detection mistakes

Written on 2005-06-01 09:35 by tarquin. Last modified 2005-09-19 09:19

If is a script is intended to be active only on certain pages, it is important to ensure that the page address is correctly detected. The simple way is to use @include or @exclude like this:

// @include http://example.com/*
// @include http://www.example.com/easy/*

or

// @exclude http://userjs.org/*

If you want a little bit more control, you can use 'indexOf' or 'match', but be very careful with using these. A URL may contain more than one page address, and it is common to cause problems by incorrectly matching pages.

Examples of common mistakes:

if( location.href.indexOf('example.com') != -1 )
  • http://www.example.com/test.html
  • http://twoexample.commands.userjs.org/
  • http://userjs.org/pagelist?reference=example.com
  • http://userjs.org/twoexample.commands/
if( location.href.indexOf('http://example.com/') != -1 )
  • http://example.com/test.html
  • http://userjs.org/pagelist?reference=http://example.com/
if( location.hostname.indexOf('example.com') != -1 )
  • http://www.example.com/
  • http://example.commands.userjs.org/
  • http://noexample.commands.userjs.org/
if( location.hostname.indexOf('example.com') == 0 )
  • http://example.com/
  • http://example.commands.userjs.org/
if( location.hostname.match(/example.com/) )
  • http://www.example.com/
  • http://example.commands.userjs.org/
  • http://noexample.commands.userjs.org/
if( location.hostname.match(/example.com$/) )
  • http://www.example.com/
  • http://example.com/
  • http://wehavenoexample.com/
if( location.hostname.match(/bexample.com$/) )
  • http://www.example.com/
  • http://example.com/
  • http://we-have-no-example.com/

More reliable ways to check the address

(Note that it is correct to escape forward slashes with backslashes inside strings and regular expressions: '/')

Exact hostname:
if( location.hostname <strong>==</strong> 'example.com' )
Exact hostname (less efficient):
if( location.hostname.match(/<strong>^</strong>example.com<strong>$</strong>/) )
All files within a given path:
if( location.href.indexOf('http://example.com/files/') <strong>== 0</strong> )
All files within a given path (less efficient):
if( location.href.match(/<strong>^</strong>http://example.com/files//) )
Exact file match:
if( location.href == 'http://example.com/foo.html' )
Exact file match (less efficient):
if( location.href.match(/<strong>^</strong>http://example.com/foo.html<strong>$</strong>/) )
Exact file match, optionally with GET variables:
if( location.href.match(/<strong>^</strong>http://example.com/foo.html<strong>(?.*)?$</strong>/) )
Exact hostname or exact subdomain:
if( location.hostname.match(/<strong>^(www.)?</strong>example.com<strong>$</strong>/) )
Exact hostname or any subdomain:
if( location.hostname.match(/<strong>(^|.)</strong>example.com<strong>$</strong>/) )
Any subdomain:
if( location.hostname.match(/<strong>.</strong>example.com$/) )
Exact hostname or exactly one level of subdomain:
if( location.hostname.match(/<strong>^([^.]+.)?</strong>example.com$/) )
Exact subdomain of specific top level domains:
if( location.hostname.match(/<strong>^</strong>images.google.<strong>(com|co.uk|com.au|no)$</strong>/) )
Exact subdomain of most common top level domains:
if( location.hostname.match(/<strong>^</strong>images.google.<strong>([^.]+|[^.]{2,3}.[^.]{2})$</strong>/) )