1

We know that using Javascript we can load CSS files.

But, can we load Javascript files or run Javascript code using CSS?

If yes, how? Is there any documentation for this?

6
  • 2
    No, luckily not. There used to be something like this in older IEs but it was a security nightmare and has gone extinct. Commented Dec 22, 2013 at 6:03
  • No. Delete this question. Any what purpose would this have? T Commented Dec 22, 2013 at 6:03
  • @Leeish It's just my curiosity... I just want to know if this is possible. Commented Dec 22, 2013 at 6:05
  • I'm not quite sure what your end goal is, but have you checked out the CSS variables that are coming (I think they just hit FF nightlies)? The other thing I guess I would ask is what you mean exactly by "loading" vs. "running" JS? Couldn't you in theory pull down a JS file by referencing it as an image or some such? I can't imagine it would help you much, but... Commented Dec 22, 2013 at 6:11
  • @JTrana It's just like an experiment... I couldn't imagine that such a thing is possible (even only in old IE versions). I don't plan to use this in a real application, but I want to know if this is possible or not. Or which are the solutions... Commented Dec 22, 2013 at 6:19

3 Answers 3

2

I think IE and firefox supports CSS expressions which probably you may think of. Something like

width:expression(document.body.clientWidth > 800? "800px": "auto" );

but these are really bad things and you should avoid this

Also you should avoid CSS expression as much as you can.

You can use the CSS like this:

body {
  behavior:url(xyz.htc);
}

and now in the xyz.htc file you can write like:

<PUBLIC:COMPONENT TAGNAME="xss">
   <PUBLIC:ATTACH EVENT="ondocumentready" ONEVENT="ABC()" LITERALCONTENT="false"/>
</PUBLIC:COMPONENT>
<SCRIPT>
   function ABC() 
   {
     alert("Message");
   }
</SCRIPT>
Sign up to request clarification or add additional context in comments.

3 Comments

Does Requiring a htc file work on newer browsers or only in the old ones?
@イオニカビザウ:- Its on the old ones but you can check it on the new ones! Also I suggest you to read the expressions link which I have linked in my answer. That would help you!
Tested in Firefox and Chromium, but it doesn't work. Anyway, interesting information. Thanks!
0

No, you can not run Javascript or import Javascript into CSS... not that you would ever need to.

Comments

0

No.

The closest thing I can think of is Netscape 4 had a thing called JavaScript Style Sheets (JSSS)

http://www.w3.org/Submission/1996/1/WD-jsss-960822

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.