Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making this a little more clear - dynamically applying stylesheet to document #194

Open
ORESoftware opened this issue Aug 14, 2015 · 6 comments

Comments

@ORESoftware
Copy link

this all looks very promising, and I am very familiar with RequireJS at this point. But what's not clear to me about your lib is how to actually dynamically add the stylesheet once it's JS memory on the front-end. So ok, say I have this:

define(['css!my_stylesheet_path',function(sss){

 //how do I apply the stylesheet 'sss' to the document?  <<<<

});
@ORESoftware
Copy link
Author

We can dynamically add stylesheets to the document like this:

<html>
    <head>
        <link rel="stylesheet" href="stylesheet.css" type="text/css" />
    </head>
    <body>
</html>
$(document).ready(function () {
    $("a").click(function () {
        $('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />');
    });
});

but TMK this will do an AJAX request to look for the file on the server, not in JavaScript runtime

@ORESoftware
Copy link
Author

Perhaps this is the answer:

http://www.w3.org/wiki/Dynamic_style_-_manipulating_CSS_with_JavaScript

var sheet = document.createElement('style')
sheet.innerHTML = "div {border: 2px solid black; background-color: blue;}";
document.body.appendChild(sheet);

This should be in your examples, no? It took me an hour to determine that it was just a matter of appending a string like that. Bah humbug.

@TheSisb
Copy link

TheSisb commented Aug 14, 2015

This lib loads and inserts the CSS into the page automatically.... It doesn't go into "JS memory"

See line 129

@ORESoftware
Copy link
Author

you mean this line:

var link = document.createElement('link');  //129?

are you aware that you can do this:

var sheet = document.createElement('style');  //1
sheet.innerHTML = "div {border: 2px solid black; background-color: blue;}"; //2
document.body.appendChild(sheet); //3

as per
http://www.w3.org/wiki/Dynamic_style_-_manipulating_CSS_with_JavaScript

'link' uses AJAX behind the scenes, so it won't work with the r.js optimizer

if you don't see what I am saying I can try to explain more, but basically what I am suggesting is to use the above 3 lines of JS to add stylesheets to the page without having to re-retrieve the stylesheet from the server.

hope it makes sense

@gpgekko
Copy link

gpgekko commented Nov 5, 2015

In development, it always retrieves from the server, the same as it does with your JS. When optimized, the CSS is included in the JS file (like with any other dependency), along with a helper script that will insert the CSS into a style tag. So it actually does everything you talk about already.

@ffflabs
Copy link

ffflabs commented Nov 6, 2015

And link doesn't use ajax behind the scenes. It's a plain old request, same as adding an image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants