61

I have loaded a css file on server so I am having a URL with me. How can i load it in my perl code using JQuery ?

So currently I am hardcoding the css in my mason page which is missing from the page something like this

JQ.onReady('show', function(){
    JQ.addStyles({styles: ["\n\n.ap_classic { border-top:1px solid #ccc;border-left:1px solid #ccc;border-bottom:1px solid #2F2F1D; border-right:1px solid   #2F2F1D;background-color:#EFEDD4;padding:3px; }  .ap_classic .ap_titlebar { color:#86875D;font-size:12px;padding:0 0 3px 0;line-height:1em; }  .ap_classic .ap_close { float:right; }  .ap_classic .ap_content { clear:both;background-color:white;border:1px solid #ACA976;padding:8px;font-size:11px; } "]});
});

I want to avoid hardcoding this css ?

3
  • 2
    Why does is have to be jQuery? Why not just output a link rel= into the head? Commented Oct 12, 2010 at 9:50
  • Not sure what you mean by load it in your perl code, do you want to modify the file? Or simply just display it on the page? If it's the latter, I would try and load it from perl/html. Commented Oct 12, 2010 at 9:50
  • 3
    possible duplicate of Load external stylesheets on request? Commented Oct 12, 2010 at 9:51

4 Answers 4

87

I don't get why you can not just insert the <link/> element in the <head/> section, but here's a jQuery snippet:

$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', 'your stylesheet url') );
Sign up to request clarification or add additional context in comments.

4 Comments

I am having a mason page. So its not a html page where i can set headers.
Was trying same... But IE version 8 does not load the CSS loaded using this method.
document.createStyleSheet('style.css'); IE FIX
Well if you are packaging a .js file for public use, you want to make setup simple and not burden the user to put a link in the head HTML
48

Again, as per Dynamically loading css stylesheet doesn't work on IE.

You need to set the href attr last and only after the link elem is appended to the head:

$('<link>')
  .appendTo('head')
  .attr({
      type: 'text/css', 
      rel: 'stylesheet',
      href: '/css/your_css_file.css'
  });

1 Comment

Doh, of course it would! I must have been a asleep when I coded that. Great amendment Sergey :)
2

I like being consistent about language and file type (ex: don't mix css with html). So I would go for creating a style.css file and loading it into a tag with jQuery.

index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <div>
        <style id="import"></style>
        <h2 class="red">this is red</h2>
    </div>
    <script type="text/javascript">
        $( document ).ready(function() {
            $( "#import" ).load( "./style.css" );
        });
    </script>
</body>
</html>

style.css

.red{
    color: red;
}

Comments

0
$(document).ready(function(){
    console.log("ready!");
var e=$("<link>",{
    rel:"stylesheet",
    type:"text/css",
    href:"/your/css/file.css"})[0];
e.onload=function(){
console.log("CSS IN IFRAME LOADED")},
document.getElementsByTagName("head")[0].
appendChild(e)});

1 Comment

I think it's generally recommended to not just paste blocks of code but include an explanation to your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.