Using .htaccess instead for the task

In the tutorial Comprehensive Guide to .htaccess, it featured a magical little file called .htaccess of your server (you may or may not have access to it, depending on your web host), and how it can enable everything from custom 404 pages, password protection of directories, to disabling hot linking of images on your server. Well, faced with our current challenge, we simply need to elaborate on that last point a bit, so instead of- or apart from- images, JavaScript libraries are included within the list of files not to be accessed from outside your site.

Take a look at the below code, which is all that's needed to accomplish the task using .htaccess:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?*$ [NC]
RewriteRule \.(js|gif|jpg)$ - [F]

Notice "js" in the last line, which adds .js files, along with .gif and .jpg to the type of files only the domains listed above can access. The code should be saved inside your .htaccess file, and dropped into your web page directory.