Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Running an External Script on Page Load

One way an external script can be run on page load is with a non-display image tag. PHP, Perl CGI, or other external software can be launched.

The trigger is with JavaScript. The image code itself is JavaScript.

I tend to use this type of code to avoid bots, generally for launching a page-load counting PHP script after the page has loaded. The script counts only page loads of real browsers that hang around long enough for the page to load.

  • Robots are unlikely to run JavaScript; therefore, they don't get counted. Those that do follow JavaScript tend to be maintained by major search engines, which can be filtered out of counts.

  • People who don't hang around long enough for the page to load don't get counted.

The result is a count of real people who have visited the page.

This article contains the code for that implementation. You may, of course, replace the PHP script with different software, a script that does what you want done when a web page completes loading — an IP address log to determine geographical distribution of site visitors, or updating a central database for determining which web pages are the most popular or time of day the site is visited most, are examples; any script that can be run afterward so it does not slow down the initial page load.

The Logging Script

The logging script is a PHP script external to the web page. It can be installed anywhere on the website.

Here is the source code. A customization is addressed following the source code.

<?php
/* Log Page Load
   Version 1.0
   Will Bontrager Software LLC
   https://www.willmaster.com/
*/
if( empty($_GET['url']) ) { exit; } // If parameter "url" is missing or empty, script was incorrectly launched.
$LogFile = "{$_SERVER['DOCUMENT_ROOT']}/logfile.txt";
$Log = array();
$Log[] = date('r');
$Log[] = $_GET['url'];
$Log[] = $_SERVER['HTTP_USER_AGENT'];
file_put_contents($LogFile,implode("\t",$Log)."\n",FILE_APPEND);
exit;
?>

Replace {$_SERVER['DOCUMENT_ROOT']}/logfile.txt with the location where you want the log file to be maintained.

Save the script as LogMe.php or some other PHP file name that you prefer.

Upload LogMe.php to your server and make a note of its URL.

The Web Page

The web page has a JavaScript function and an independent JavaScript command.

Let's start with the JavaScript function that launches the external logging script.

<script>
function LaunchScript()
{
   var holder = new Image();
   holder.src = "/LogMe.php?url="+document.URL;
}
</script>

The LaunchScript() function creates a new variable to hold an image. Then it loads the image — which is the script you are launching. The "image" is never displayed.

Replace /LogMe.php with the URL of the script you are launching. It may be a relative URL or an absolute https://... URL.

Now, here is the independent JavaScript command.

<script>
document.addEventListener("DOMContentLoaded", function() { LaunchScript(); }, false);
</script>

There is no customization there.

Both the JavaScript function and the independent JavaScript command can be inserted into your web page source code wherever JavaScript can run. Immediately above the closing </body> tag is fine.

As an FYI, the Javascript function and command can be in the same script tag. As:

<script>
function LaunchScript()
{
   var holder = new Image();
   holder.src = "/LogMe.php?url="+document.URL;
}
document.addEventListener("DOMContentLoaded", function() { LaunchScript(); }, false);
</script>

You are good to go. Test it, of course to ensure the JavaScript runs as it is supposed to and the external script runs without errors.

As noted, the external script may be any script you want to run when the page finishes loading.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC