开发者

Add Javascript to Every Page in a Website via CSS?

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 e开发者_运维知识库very 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜