Add Javascript to Every Page in a Website via CSS? - html

Is there a way to add a Javascript segment to each page in my website via CSS?
I am looking to add some Javascript tracking code (for my own benefit (using Google Analytics)) before the closing </head> tag on every page.
The simple question is: is there a way to add code to the HTML on every page of my website between the <head> and </head> tags using CSS to dynamically insert it (as opposed to going through and updating every page manually)?
Thank you,
Mick

No, CSS is a collection of styles to apply to elements (which can handle alternating states of controls, such as :hover), but isn't functional in the way you desire.
However, it seems to me that, if you can reference a CSS file globally, then you could simply place your Javascript in a separate file and reference that in each of your pages with a <script> tag (similar to how you would do with CSS files.)
Furthermore, if you're using a framework such as ASP.NET, then you can use master pages as templates from which relevant pages derive, reducing redundancy of this sort of thing.

Is there a way to add a Javascript segment to each page in my website via CSS?
No. Use a template language (e.g. TT) or include system (e.g. SSI) if you want common HTML across pages.

Nope, you can't add javascript through css. These are different techniques, one for changing the look of your website (css) and one for adding functionality to it (javascript). There is no way to add javascript through css-code.

As all the other answers stated: No, in general not.
Only MS IE offers such a solution, using the behavior-property.
I currently use this for an hover-anything-script for IE6 (included inside a conditional comment), which allows to add the CSS-:hover-selector to any HTML-element:
body {
behavior: url(/js/csshover.htc); }
The csshover.htc is from Peter Nederlof (LGPL).
See also: MSDN HTC Reference

Others have already noted that it's impossible. It seems that your site is static html only. Then you could write a little script that would prepend a link to javascript file before

Related

Hypothetical question regarding styling <body> and <html> differently using the same stylesheet

I hope all is well.
So, currently I am using a self-made template from dreamweaver (which includes a header and a footer) and I use this template to create new pages from it.
Many tutorials style the body and/or html tags inside the stylesheet, which would be fine if you were only creating one page, however I have multiple pages attached to one stylesheet.
So, by following any of the tutorials mentioned above It would completely change the layout on my other pages since they are all linked to the same stylesheet, is there any way around this?
I hope this makes some sort of sense,
Thanks in advance, Mark.
Assuming I understood your question correctly, you can link the same style sheet to multiple HTML pages. You'd just have to write your CSS accordingly.
Example:
.PG1-BOX,
.PG2-BOX{
}
Furthermore, keeping all your external CSS in one style sheet is better for maintainability and reduces loading times. If you had multiple style sheets, the user has to request them all and then download them all whereas maintaining just one stylesheet would reduce this process.

How to find CSS related to specific page?

Is there any way to find which CSS tags and elements are related to the specific HTML page section?
For example: We have a large CSS file but few of elements are related to content in HTML classes, is there any way to find related elements and remove other parts?
How large is the CSS file? I can't think of anything else than splitting it to multiple libraries and then including a library in your page if it's required.
For example, if the page has a slider then it will have a CSS link to slider.css this might not be ideal when you send too many HTTP requests so you might embed the library as internal <style> CSS. I've seen many WordPress theme developers use that technique.
This shouldn't be a problem since minification and Gzipping reduce the filesize drastically and once the file is cached your users wouldn't need to wait extra time to load your next pages.
For Chrome
In the Chrome DevTools, there is an Audits tab that will allow you to run a Web Page Performance audit and see a list of unused CSS rules :
For Firefox
You could install one of these add-ons :
CSS Usage for Firebug
Dust-Me Selectors
CSS Usage
Dust-Me Selectors
I don't think there is. It would be really hard to do this, too, because your system could build up a page by including 500 different files of different languages. How could such a program know which of your files is included where and under which conditions?
The thing that I think comes closest would be using your DevTools to see which styles apply to which element and maybe by hand or in an automated way create a list of which CSS rules apply to which end-page (for example which URL endpoint) element. But! Even then it would be hard, because it would be really hard for a program to find out which styles are dynamically added to elements (for example Javascript could add/remove classes when a user performs a certain action).

Bookmark in HTML/CSS

Is there a way to Bookmark using HTML/CSS? I have been developing this mobile application in HTML and want to be able to give the option to bookmark certain pages to the users. Is there a way to do this in HTML?
There isn't a way in pure HTML (since it's just a markup language) or CSS (this just define the style), since bookmarking is an action done exclusively by the browser without iteration with the web page. I'm aware that JavaScript can do it.
I don't think so, as HTML is only a markup language, not supposed to interfere in browser's data or configuration. In case JavaScript is also an option for you, please refer to this answer.

CSS and HTML - Can CSS files generate HTML?

I am making a website that is styled using CSS, in two DIVs. One 'Header' DIV which is always the same, and a 'Content' DIV that changes.
In my CSS file, is it possible to write the HTML with all the links that stay in the header, so I just need to call (or similar) on every page, instead of having to write out my header content every time? Would also help in editing only one source, as I often leave out pages by mistake.
I don't want to use frames, so looking for an alternative.
Thanks,
Brett
Depending on the server and server side languages supported, this can be done.
Some servers will let you use Server Side Includes, for example. With others you could specify "block" of HTML for a header (for instance) that will be part of a site wide template.
What is not possible it to achieve this with purely CSS and HTML.
This is why you usually have some server side code running, which will insert the common header e.g. php include, SSI or other templating framework. If that's not an option, you could write JavaScript which writes out your header each time to a specific DIV. Although I don't think that's great from an SEO perspective.
The usual options for doing this client-side are an iframe, or some javascript that does DOM to add content (perhaps loaded from an external file). Or some javascript that creates an iframe. Or an iframe that creates some javascript. Some permutation of those odious techniques.
CSS does have the content property, but i think it's limited to plain text. I don't know if you can use it to pull in HTML, either using a string or URI.
As others have mentioned, the most common approach is to do it server-side. You can do this bottom-up with includes, or top-down with things like Tiles and SiteMesh.
CSS can't help you to do this.
You can make ajax loading of content. You can make one index.html and lot of 'content' files (about.html, contacts.html, etc). And in index.html you can load another .html in content-div (for example with jQuery method .load())
Another way - you can make little templating engine in php (or another server-side language)
It isn't possible to achieve that using CSS, because CSS can't handle any events, it's simply a "refrence" for the browser to know how to style your web-page, it's done, however, using AJAX.
I suggest you to take a look on jQuery lib, it'll speed up the process tremendously, however - the main disadvantage is that the search crawlers won't be able to index your page correctly, so it'll be bad by an SEO perspective.
It's possible, if you have the time, to make an index-able version and an AJAX one, that's what we did for a mobile project here.
jQuery AJAX API

Is there a way of including HTML pages without needing any javascript or server-side code, only HTML

Is there a way of including HTML pages without needing any javascript or server-side code, only HTML.
Basically I can't be sure if the server supports server side includes. I don't want to use javascript and I don't want to use any PHP or other server side functionality.
Is there any way to do this. At the moment I suspect not, but would be very interested if it were possible.
Use some frames in your page
HTML frames allow authors to present
documents in multiple views, which may
be independent windows or subwindows.
<iframe></iframe> is the tag you need to include inline floating frames.
Quick tip with iframes: Be sure you open and close the tag explicitly, if not some browsers will complain
You can use framesets.
HTML imports, part of HTML Components, aims to do exactly this.
HTML5rocks offers a great tutorial to get started with HTML imports.
According to can I use, only Chrome is supporting HTML imports today.