logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Addin a script to the header tag of a page
Author
NogNeetMachinaal



Joined: 16 Feb 2014
Posts: 9
Location: Netherlands

PostPosted: Sun 16 Feb '14 13:45    Post subject: Addin a script to the header tag of a page Reply with quote

Hi Everyone,

Please allow me to get a bit off-topic here since I don't know where to drop a question like this.

For the purpose of performance monitoring, I'm looking for a way to add a script in the header tag of a web page without using a CMS or anything like that. Ideally, it should be the first script that is running when the page is rendered.

Anyone that can help me doing this in Apache; ideally also for Tomcat?

I'm running Apache 2.2.25 and Tomcat 7.0.50 - both Win32 versions.

There are two reasons for an approach like that.
(1) - I expect it to work regardless the CMS I'm working with; the same expectation is for Tomcat and Java applications.
(2) - I'm able to start this as early as possible => includes monitoring the performance of the CMS itself.


Thanks - Will
Back to top
Anaksunaman



Joined: 19 Dec 2013
Posts: 54

PostPosted: Mon 17 Feb '14 11:08    Post subject: Addin a script to the header tag of a page Reply with quote

Since you mentioned
Code:
"Ideally, it should be the first script that is running when the page is rendered."

I would assume you mean in standard HTML so that you have something like:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>A document with SCRIPT</TITLE>
<META http-equiv="Content-Script-Type" content="text/tcl">
<SCRIPT type="text/vbscript" src="http://someplace.com/progs/vbcalc">
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT type="text/javascript">
...some JavaScript...
</SCRIPT>
</BODY>
</HTML>

(Taken from http://www.w3.org/TR/html401/interact/scripts.html)

Just place the script you'd like to use first before any other script. Depending on if you use a CMS, some modification of your template may be required to inject the script before the rest of the CMS page is rendered.

If you are speaking about actually modifying/interrogating Apache server http headers, you may want to look into mod_headers:

Code:
Example: Add a header, MyHeader, to the response including a timestamp for when the request was received and how long it took to begin serving the request. This header can be used by the client to intuit load on the server or in isolating bottlenecks between the client and the server.

Header set MyHeader "%D %t"

http://httpd.apache.org/docs/2.2/mod/mod_headers.html

This certainly could be used in conjunction with some other method of performance monitoring.

To modify headers directly for Tomcat, you will likely need to use some combination of a servlet and filter/filter-mapping directives.
Back to top
NogNeetMachinaal



Joined: 16 Feb 2014
Posts: 9
Location: Netherlands

PostPosted: Mon 17 Feb '14 12:46    Post subject: Reply with quote

Thanks Anaksunaman!

Your assumption is correct.
I would like the script to be part of each html page that is served => be part of the standard html.

So I would expect something like:
    <SCRIPT type="text/javascript" src="http://someplace.com/progs/BrowserPerformance.js"></SCRIPT>

Which is than to be added somewhere in the Apache configuration or a template file.

Assuming this is a valid approach, what file(s) would that be?


Thanks - Will
Back to top
Anaksunaman



Joined: 19 Dec 2013
Posts: 54

PostPosted: Tue 18 Feb '14 12:55    Post subject: Add a script to the head tag of a page in Apache Reply with quote

NogNeetMachinaal wrote:

I would like the script to be part of each html page that is served => be part of the standard html.

So I would expect something like:
    <SCRIPT type="text/javascript" src="http://someplace.com/progs/BrowserPerformance.js"></SCRIPT>
Which is than to be added somewhere in the Apache configuration or a template file.

Assuming this is a valid approach, what file(s) would that be?

Actually, there are a number of ways to accomplish this.

NogNeetMachinaal wrote:
(1) - I expect it to work regardless the CMS I'm working with; the same expectation is for Tomcat and Java applications.
(2) - I'm able to start this as early as possible => includes monitoring the performance of the CMS itself.

Since you are using Apache 2.2.25, I think the simplest way for you to accomplish this would be a combination of mod_ext_filter and mod_substitute.
My memory is a bit patchy at times for please forgive any mistakes. Smile

You didn't specify OS, but for Apache 2.2.7/2.2.12+ the process should be something like:

1) Make sure mod_ext_filter and mod_substitute are available -- that is, you have compiled versions of them available to Apache.

2) In your Apache configuration file (httpd.conf on Windows and likely apache2.conf on Linux, depending on distro), make sure
Code:
LoadModule ext_filter_module modules/mod_ext_filter
[...]
LoadModule substitute_module modules/mod_substitute
are uncommented.

3) You may need to add
Code:
AddType application/x-javascript .js
to the end of your configuration file as well.

4) Add the following to your site configuration in Apache:
Code:
<Location / >
 AddOutputFilterByType SUBSTITUTE text/html
 Substitute "s|<head>|<head><script type=\"text/javascript\" src=\"http://someplace.com/progs/BrowserPerformance.js\"></script>|ni"
</Location>

If you are using just a single site for the server, your will likely need to add this to httpd.conf or apache2.conf, as for steps 2 and 3. However, if you use Apache virtual hosts, you will add these lines to whichever virtual host container(s) you want to have the script load for (that is, for every site you want to monitor). For example:

Code:
<VirtualHost *:80>

ServerName example.org

ServerAlias www.example.org
DocumentRoot "C:/web/www/domains/example.org"

<Location / >
 AddOutputFilterByType SUBSTITUTE text/html
 Substitute "s|<head>|<head><script type=\"text/javascript\" src=\"http://someplace.com/progs/BrowserPerformance.js\"></script>|ni"
</Location>

</VirtualHost>
Back to top
Anaksunaman



Joined: 19 Dec 2013
Posts: 54

PostPosted: Wed 19 Feb '14 3:58    Post subject: Add a script to the head tag of a page in Apache Reply with quote

Some notes about the above Apache approach:

http://httpd.apache.org/docs/2.2/mod/mod_substitute.html

* What is happening above is a straight text replacement. The <head> tag is actually being replaced by Apache before the document is served to the client with <head><script type=\"text/javascript\" src=\"http://someplace.com/progs/BrowserPerformance.js\"></script>.

* The s prepend portion of the substitution is standard syntax, the pipe characters are delimiters between parameters (normally they are forward slashes but we use pipes instead because our substitution already contains forward slashes and thus things would get confusing) and "ni" are options that indicate parameters should be treat as string literals vs. regular expressions and be treated case insensitive ("n" and "i"respectively).

* The whole thing is enclosed in quotes because there are spaces in the substitution, and we thus have to use backslashes to escape our extra double quotes in our substitution string.

* <Location /> </Location> makes it so every page of the site is edited with this script. You may wish to change this behavior if this is undesired for some reason.

* If you need a quick test, insert the following into a text file and save it as a .js file:
Code:
alert("Apache Insertion!")

You should get a dialog box every time you load a new page for the site with the banner text. Of course, make sure the .js file name is the same as in your substitution directive!

* Loading scripts at the <head> is discouraged if the script is "heavy". That is, page blocking can occur until the script is finished, so unresponsive scripts can hurt page loads. Since yours is a performance script, you may want to make sure it is functioning correctly and not messing with your page loads. You should place other scripts further down the page in your HTML.

* For Apache 2.4+, to do the same thing, you need mod_filter, not mod_ext_filter, and you shouldn't need to add the extra document handler.
Back to top
Anaksunaman



Joined: 19 Dec 2013
Posts: 54

PostPosted: Wed 19 Feb '14 4:03    Post subject: Add a script to the head tag of a page in Apache Tomcat Reply with quote

For Tomcat, the process is potentially more complicated. If Apache is actually serving the page and Tomcat is simply used as a back end for content generation or suchlike, as long as Apache renders the final page, you should be OK. If you are using Apache as a proxy to Tomcat, etc., as I indicated previously, you will likely need a more complex setup.

Here is a link on Stack Overflow which sums up the basics for a similar set up in Tomcat, if Tomcat is more the primary serving application --
http://stackoverflow.com/questions/14736328/looking-for-an-example-for-inserting-content-into-the-response-using-a-servlet-f
Back to top
NogNeetMachinaal



Joined: 16 Feb 2014
Posts: 9
Location: Netherlands

PostPosted: Thu 20 Feb '14 9:55    Post subject: Reply with quote

A big thank you for your help Anaksunaman!

I will give this a go in the coming weekend and let you know the outcome.

Cheers - Will
Back to top
scotia



Joined: 14 Dec 2017
Posts: 1
Location: Melbourne, Australia

PostPosted: Thu 14 Dec '17 13:10    Post subject: Reply with quote

Thanks for a great solution Anaksunaman. I can confirm that your solution works. I am using Apache 2.4 with vhosts.

Note that do this within a vhost for the entire namespace the <Location> tag is not required.

Scott
Back to top


Reply to topic   Topic: Addin a script to the header tag of a page View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner