How to add some HTML to every HTML document served? - javascript

I need to add a script before the </body> tag, so instead of modifying all the pages, I was wondering if I could configure the server to add some HTML before that tag for every HTML document being served?

If you have PHP installed on your server, you can set up the auto_append and/or auto_prepend directives in the php.ini file or .htaccess. You can also set up .html extensions to be parsed as PHP files by Apache, so every HTTP request for an .html document is sent back with a header and a footer automagically included. If PHP is set up, try adding these lines into your .htaccess:
AddType application/x-httpd-php .html
php_value auto_prepend_file /var/www/public/foo.html

Apache can handle that using mod_layout
Here's a relevant article: Advanced Apache Headers/Footers

The most natural answer to your problem would be to use a server-side processing language such as PHP, CGI, etc. Those platforms give a lot more than just server-side includes. Speaking of which, if including something in an HTML page is just really what you need, you might be looking for Server Side Includes.

Related

Javascript caching and load if modified

I'm developing widget based script based on javascript, where the end user will come to my site copy the script from my widget site something as follows and paste into their site.
<script src="text.js"></script>
The problem im facing is if i modify anything in test.js file from my side, the changes is not reflecting in users site because of js file is being cached users browsers.This test.js is hosted in apache server.
Im have tried many solutions using .htaccess to set Etags,Cache-control but none of them work. Is there any headers that i need to add/modify in .htaccess. The solutions im looking for is request should check for last modified and load the new js if modified, else load from browser cache(Preferably handle with .htaccess).
This is one of the sample .htaccess i tried .
Header set Cache-Control "max-age=864000, public, must-revalidate"
FileETag MTime Size
Regards,
Karthi Kumar

How can I configure apache to use php to create a javascript file

I want clients to be able to include one of my javascript files on their site like this:
<script src="http://www.example.com/blah.js" ></script>
But I want to actually use php to dynamically create the javascript file/response (I don't want blah.js to actually exist on my server). I am running ubuntu 14.04 and apache 2.4.7. blah.js is the only file I want my server to act on like this. In my php I will check the IP address and create the javascript file/response based on the client site. I want any other javascript file request to act as normal.
Can I use .htaccess to override the directory to have any request for blah.js to be handled by php?
Use rewrite in your htaccess :
RewriteEngine On
RewriteRule ^blah\.js /blah.php [L]
When you will call /blah.js, it will be redirected to blah.php
You can also use .htaccess auto_prepend_file directive to prepend .php file before any of Your other php files come into play, and than use that prepended script to generate whatever You might need.
In Your .htaccess file:
php_value auto_prepend_file "/my/loc/to/php/file.php"
There is also auto_append_file which will pull particular script just after last one manually included by You or application, and do the job after all content is generated, before headers are sent to the client.
Note that php_value directives are not enabled on each shared hosting setup.

How do I link JS external files outside of my PHP project root directory?

On my site I have my resources folder outside of the root, for example:
/var/www/html/ is the root directory
/var/www/resources/
I currently have a config file that sets the location of the library so I can include it with php like so:
defined("LIBRARY_PATH")
or
define("LIBRARY_PATH", realpath(dirname(__FILE__) . '/library'));
which works perfectly when I use:
<?php include_once(LIBRARY_PATH . "/file.php"); ?>
but it doesn't work when trying to add Javascript files:
e.g.
<script src="../resources/library/js/test.js"></script>
links to 'www.website.com/resources/library/js/common.js'
or
<script src="<?php echo LIBRARY_PATH; ?>/js/test.js"></script>
links to 'www.website.com/var/www/resources/library/js/test.js'
neither of which work.
Any suggestions on how I can do this without having the js files in or above the root?
Your JavaScript files have to be accessible to the browser because they are executed by the browser and not by the server.
This requires that they have a URL.
Putting the files under the webroot is the standard way to give a static file a URL.
Alternatively, you could write a program (e.g. in PHP) that will read the file and then output it's content to the browser. This is more complicated and makes dealing with cache control headers more fiddly and is not recommended.
Assuming you understand what you're doing and security implications of that!..
You create the linkjs.php script that takes the relative path to the script (from some root dir, perhaps /var/www/resource/js) as a parameter, like:
<script src="/linkjs.php?p=test.js">
In your PHP script you resolve the full file path, check that it's indeed a file under the root dir (to protect against ../ in the parameter), that it's readable by you PHP user, read the content and output it into the response. Don't forget to set content type to text/javascript of course.
Ideally, you should also provide proper caching headers based on the source file modification time, but that is a topic in itself. See the guidelines in other SO questions about proper caching headers for dynamic content.
The upside is that you can do on-the-fly script minification/combining/wrapping/substitutions if you like/need.

uploading site files, index.html or home.html

just about to upload my first site and have just been doing some research into how to do this whilst I await my package to be upgraded to include mysql databases.
Quick question - do I need to name my homepage 'index.html'?, and therefore change every 'home.html' link on every page of my site to 'index.html'? I've also heard 'index.htm', what is the difference between html and htm?
From what I have read, this is good practice (well, even better practice would have been to call it that from the start!) but also that you can change the default page in the htaccess file. Is this correct, and if so, is there a downside?
I am not relishing the task of changing every homepage link, but if it is considered a better option, I will.
Thanks
Yes you can change the default index file and even the ordering. Some options are:
a) You are using server side scripting - php for example
Add this into a .htaccess file
DirectoryIndex index.php index.html
b) You are not using any server side scrpting
DirectoryIndex index.html
This way you do not have to deal with default values such as home.htm or index.htm
Browser will automatically open the "index" file. Though its true - you can redirect the browser to your home.html but it is always a better option to name your start-site index.html
about the difference between HTML and HTM... There is none.
Years ago, in DOS times, extensions were only 3 characters long 'htm'.
Now, we got longer extensions, like 'html' with 4 characters.
It's preferred to make it as index.html as index.html is a better practise than home.html.
Yes, you can change the index file via .htaccess and .html and .htm has no difference, it's just different extensions for the same HTML.
Yes you can have home.html, however, you will have to set your default site preferences in your web server to serve that instead of the default "index.html".
Whilst there is no difference it would be beneficial to have index.html, as it is the default and it will help you reduce web server config time if you are using clean-urls e.g. domain.com/contactus, domain.com/aboutus, etc instead of domain.com/contactus/home.html and domain.com/aboutus/home.html, etc.
Lastly, there is no difference between .html and .htm it is just a preference thing.

include .html in a .html

I used the script
<!--#include virtual="dontate.html" -->
to include a .html file into another .html file, but for some reason does not show up.
Any reason as to why?
I will be using WAMP and LAMP.
Thanks
Jean
This is using server-side includes. You need to set it up on your server. Despite the misleading "comment" syntax, it really has nothing to do with HTML.
if there's php on the server you can just include the html file like so
<?php include "donate.html"; ?>
A lamp stack has php so you should be set.
However, the server may not be set up to run the file through php's interpreter. You can either set it to parse whatever the extension of the file is (being the file that is doing the including) or change it to something that is already set to be parsed (home.html -> home.php)
As your using wamp/lamp I found something that will work:
creating a .htaccess file and put the following in it
Options FollowSymLinks Includes ExecCGI
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
Then change your html file extensions to .shtml (including the file that is to include a file)
So
<!--#include virtual="dontate.html" -->
Would become
<!--#include virtual="dontate.shtml" -->
This should work
NOTE:
The file is in the same directory as the file that is including it use:
<!--#include file="dontate.shtml" -->
If it is in a different directory use:
<!--#include virtual="folder/to/file/dontate.shtml" -->
Perhaps the problem is the use of virtual
The file parameter defines the included file as relative to the document path; the virtual parameter defines the included file as relative to the document root.
http://en.wikipedia.org/wiki/Server_Side_Includes
<object data="file.html" width=400 height=200"></object>
If memory serves... I recall some border issues in IE but that should be sufficient for a primitive integration.
As per the HTML spec, the only way to do this without server side scripting or server side includes is via iframes.
* note the bolded 'only'.
Guys..here is the answer
// jsfile.js
var variable=""+
"";
document.write(variable);
****include .js file in html****
thats it

Categories