While the functions that I used in the original version of my falling snow script back in 2004 would still work in the new script, I have discovered ways of making that code much shorter since I originally created functions to provide cross browser support for accessing such things as the size of the viewport and the scroll position of the page within the viewport.
In my original falling snow script I used the following four functions to retrieve the width and height of the viewport and the X and Y distances that the page had been scrolled.
The new version of my falling snow script provides the same functionality by defining one object and one function that returns an object.
If the page is larger than the viewport then it is quite likely that it will get scrolled so simply determining that the page has scrolled a distance of zero in both the X and Y directions at the start is not going to be sufficient. Both versions of the script therefore need to calculate the scroll position dynamically at regular intervals while the script is running. The new version of my code therefore uses one function that can be called to return an object with the two values we need rather than having two separate functions.
As you can see, the code from the new version of the script is much shorter than the code from the old version that is above. Both of these are still doing exactly the same thing but with the new version making use of objects to identify relationships.
The amount of code required to actually use the above code to get the width and height of the viewport doesn't need to change much. Instead of the following two lines:
we now have the following:
About the only difference this makes is that the new version is slightly more obvious as to exactly what the values are.
The change to the code for handling the scroll position is also not all that great. In this case we do need to add an extra statement that calls our new function and gives a name to the object that is returned so that the properties can be referenced in place of the separate function calls but again the difference isn't all that great. We replace this code from the old script:
with the following in the new script:
None of these changes were strictly necessary as the functions in the old script for accessing the browser object model but when we are updating our scripts to replace the code we needed for older browsers with the more efficient code that newer browsers will allow us to use, we may as well replace any functions that we are using from our function library with the latest versions that we have.
This article written by Stephen Chapman, Felgall Pty Ltd.