Output CSS from javascript and then make a callback
My dilemma is this, i'm going to write a php script that will write CSS to a javascript file. I need this JS file to, when called as a css file, output just the css. I then need to make a get request from the same JS file to an endpoint back on my server. Now my specific questions are: how exactly c开发者_如何转开发an i output pure CSS from a JS file. And if i make a get request in the same JS file will this interfere with the outputting of the CSS?
You can't serve CSS from a JavaScript file. Serve your CSS and JavaScript from two separate locations.
You can have your JS file output pure CSS by doing something like:
document.write("<style>.rule1 { ... }</style>");
But, I have to ask, why not have PHP directly output CSS?
Get the css string using htmlobject.style.cssText
.
If you want to get a stylesheet, use the StyleSheet object. Hints:
StyleSheets (Array-like) object document.styleSheets
CSSRule (Array-like) object document.styleSheets[0].cssRules
CssText (string) document.styleSheets[0].cssRules[0].cssText
Use document.styleSheets[0].insertRule
if you want to add some CSS strings, use document.createElement("style")
if you need to create a StyleSheet object when the document doesn't have one yet.
精彩评论