0

Part of the HTML page.

  <head>
    <title>Overview</title>
    <link href="mystyle.css" rel=stylesheet type="text/css">

I know it's unusual question, but is it possible to add a JavaScript code to mystyle.css ?

Edit1: The HTML is generated by third party software that I have no control on it. Only the CSS file is static and does not change (I thought I could add a JavaScript to be executed from the CSS file). in other words can I add this to mystyle.css

//CSS declaration... 
<script src="myjs.js">
M'hamed
  • 2,508
  • 3
  • 24
  • 41

3 Answers3

0

It is possible in some limited ways - see Using Javascript in CSS . You can also write out css rules via javascript: http://davidwalsh.name/add-rules-stylesheets . Or you can create two different css classes and apply them to elements via javascript, e.g. with jQuery's http://api.jquery.com/addclass/ or a native javascript equivalent.

I'm guessing you probably want to use variables within your css so you don't have to repeat css in several different places? Of all the techniques, if I could take a guess at what will really help you achieve what you want in a way that you won't regret later, I would recommend a css preprocessor like http://lesscss.org/

Community
  • 1
  • 1
Jim Grady
  • 210
  • 1
  • 3
  • SASS, SCSS, and Stylus are also good preprocessors to consider. – Nicholas Cloud Oct 04 '14 at 21:11
  • The first link you give is five years old and contains solutions which were deprecated even at the time. In any case, the OP's question was not how to create CSS with JS, it was if he could include JS in a CSS file, the answer to which is quite simply, no. –  Oct 05 '14 at 07:34
  • Sorry if I was not clear that the ways listed first were not recommended, that's what I meant by calling the last way the one you "won't regret." When I ask a question, I like to hear a little context about what people have tried in the past but ultimately decided against, and I like people to try to help me achieve what I'm trying to achieve instead of just saying no to what I literally asked, but maybe not everybody prefers that. – Jim Grady Oct 05 '14 at 19:30
0

The answer is no.

Why?

Because you've been too late to the party

When was the "Party"?

The "Party" ended with the arrival of IE 10. They stopped using the "htc" hack.

Any other "Party" existing?

Yeah, there's the Firefox's XBL (XML Binding Language), but again, it'll just be for firefox.

You more or less can't do anything here.

Community
  • 1
  • 1
kumarharsh
  • 18,961
  • 8
  • 72
  • 100
-2

you can, but you have to make tags with <script text...></script> I dont see a problem why wouldnt call it, but it would ruin organization. So better make seperate file and include it in html page.

benzo
  • 421
  • 1
  • 6
  • 22