4

Is it possible to use a CSS import file to import JavaScript pages?

@import url(Script.js);
3
  • 1
    What would you want to achieve with this? Commented Dec 25, 2011 at 17:26
  • 1
    Why in the world should that be possible? :P Commented Dec 25, 2011 at 17:27
  • @SergeiTulentsev My site only imports one URL in select pages. I have JavaScript for those pages but cannot add it in... Commented Dec 25, 2011 at 17:32

4 Answers 4

11

The spec states the URL has to point to a style sheet:

The '@import' rule allows users to import style rules from other style sheets. Any @import rules must follow all @charset rules and precede all other at-rules and rule sets in a style sheet. The '@import' keyword must be followed by the URI of the style sheet to include. A string is also allowed; it will be interpreted as if it had url(...) around it.

CSS3 Syntax Specification: http://www.w3.org/TR/css3-syntax/#import

Sign up to request clarification or add additional context in comments.

Comments

5

No, this syntax is not supported.

You can, however, use the src attribute of the <script> element to specify the location of an external script resource:

<script type="text/javascript" src="Script.js">
</script>

2 Comments

I know that, I'm just asking since it would be cleaner to have one file
Well, since you want to reference an external script, you will always end up with at least two files... or am I missing something?
3

No, you can only import CSS stylesheets or .less and .css stylesheets when using Less. You can do it the other way around however and import CSS stylsheets with JavaScript.

Comments

-3
// Storing HTML code block in a variable
var codeBlock = '<div class="content">' +
                    '<h1>This is a heading</h1>' +
                    '<p>This is a paragraph of text.</p>' +
                    '<p><strong>Note:</strong> If you don\'t escape "quotes" properly, it will not work.</p>' +
                '</div>'; 
// Inserting the code block to wrapper element
document.getElementById("wrapper").innerHTML = codeBlock

How about this?

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.