Current Page References

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 Javascript 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 take a look at what the address of this page is and also at what fields we might want to reference separately within that address.

The six fields described in bold have been extracted from the address of this page and made available to be separately referenced by any JavaScript code on this page (such as the script that displays the above details. In most cases you will be able to test which page you are on using uri.page which contains the page name extracted from the address but in some cases you may want to test uri.file which contains the entire filename instead. If you have multiple folders/directories within your site you may also need to test uri.path and if you have mirrored copies of your site and need to test which one you are on you may want to check uri.dom.

To be able to access the parts of your web page address using the uri object, you need to copy the following code and place it in your script above where you want to use the uri object that it creates for you.

JavaScript

You can then reference the uri object properties from any of the code that you place after that last assignment. For example, if you have some JavaScript code that you only want to run on one page (let's say "mypage.htm") then you would code an if statement around the code as follows:

if ("mypage" === uri.page) {
// code that only runs on mypage.htm goes here
}

If you have some JavaScript code that you want to run on every page except the home page then you would code an if statement around the code as follows (note that uri.page is set to index if the page name is not present in the address to make testing for the home page easier, uri.file will still be blank if you need to distinguish whether it is actually present or not):

if ("index" !== uri.page) {
// code that only runs on mypage.htm goes here
}

If you have some JavaScript code that you want to run on every page that is in sub directories on your site and different JavaScript code for the domain name itself then you would code the following:

if ('' !== uri.path) {
// code that only runs for pages in sub directories
} else {
// code that only runs for pages within the domain itslf
}

This last code might be particularly useful for referencing the same images from both places by setting a variable to '../' for sub directories and to '' for the main domain.

I am sure that you will be able to figure out other uses for these fields and how to code them for yourself.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate