1. Home
  2. Computing & Technology
  3. JavaScript

Current Page References
Part 1 : Introduction

By Stephen Chapman, About.com

If you have created an external Javascript then you may want to include that Javascript into multiple pages on your site. This is easily enough done with external Javascripts by just linking the script into all of your pages in the spot where it needs to go.

The only problem with this comes if you have some processing in the Javascript that you don't want to run on certain pages of your site or which you only want to run on certain pages. Rather than split up the content of the Javascript file so that you can include just those parts that particular pages require (and perhaps make it rather messy for the bulk of your pages that do need to run everything) you can test for the particular page details from within your Javascript so that the Javascript processes differently for the different pages.

The entire address string is available for you to reference from within your Javascript code but if you have internal anchor points and/or are passing query strings then testing the entire address field to work out which page you are on can get quite complex. To resolve this you will want to strip out just part of the address and test that.

Doing this just became a whole lot easier because I have written a function that will strip almost all of the sections of the address field into its component parts making testing of just the part that you need a trivial task.

Let's start by looking at what fields the getURL script extracts for you.

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Script Library
  5. Function Library
  6. JavaScript Current Page References >

©2009 About.com, a part of The New York Times Company.

All rights reserved.