Variables in CSS / Codeigniter? - javascript

Hey, I'm trying to set variables in my CSS so that I can allow subscribers to customize the colors of their page. I'd rather have as many files on my static server as possibly.
Is there a good / easy way to do this? Should I just have a CSS file containing all the editable parameters in a CSS file inside the application?
Walker

There are a few different ways you could do this:
Use PHP to output your CSS file that contains all the editable CSS values (make sure you remember to set the mime type of the file to text/css).
Alternatively, you could tell the web server to parse CSS files for PHP code (Apache in the example below).
This goes in your site's .htaccess file (tells the server to look for PHP in CSS files):
AddType application/x-httpd-php .css
This goes at the very top of your CSS file so browsers treat it as a CSS file:
<?php Header ("Content-type: text/css");?>

Related

To unset a javascript file in contao

I have got a javascript file which is in core folder and I want to unset that file without editing the core. I haved added
$GLOBALS['TL_JAVASCRIPT'][] = 'system/modules/lazy-images/assets/lazysizes-gh-pages/lazysizes.min.js|static';
in my template file. And this file got minimized and is present in assets folder. But the same file is present in head tag.
<script src="system/modules/lazy-images/assets/lazysizes-gh-pages/lazysizes.min.js" async></script>
I need to unset this file. How this can be done?
This JavaScript is added via the config.php of that extension. See https://github.com/derhaeuptling/contao-lazy-images/blob/2.0.1/config/config.php
To unset it, you would simply need to search for 'system/modules/lazy-images/assets/lazysizes-gh-pages/lazysizes.min.js|async' within $GLOBALS['TL_JAVASCRIPT'] within the config.php of your own extension (wich is loaded after the lazy-images extension) and unset that array key.
However, it's probably not wise to do that. You should not combine this script with the others.

Possible to have browser bypass content type when loading CSS file?

I have a web server on a device (proprietary) that serves all files as:
Content-Type:text/html
I would like to use it to host CSS and JS script files. Is it possible to have the browser ignore the content type and load the file as CSS or JS when using:
<link rel="stylesheet" href="css/main.css">
Is there no way you can override the content type from the file itself? E.g. if you put this in a php file the content-type should be altered:
<?php header('Content-type: text/css') ?>
You probably aren't using php, but perhaps something like that is possible on your web server.
In any case you should be able to use JS-files with a text/html content-type anyway. Now stylesheets ... I guess you could load them into your html dynamically if there really is no other way.
Using jQuery because I'm lazy:
<style id="dynamic-css"></style>
<script>
$('#dynamic-css').load('css/main.css');
</script>

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.

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

How to add some HTML to every HTML document served?

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.

Categories