Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm making a widget that will be added to external websites, and I have made a page that generates css for them to style it (text color, background color, font size, etc). I end up with a textarea filled with css for them to copy/paste to their website.

Is there a way to add this css to the current page in order to have a live preview?

share|improve this question

4 Answers 4

if you want to add css text

var style = document.createElement('style')
style.type = 'text/css'
style.innerHTML = 'content'
document.getElementsByTagName('head')[0].appendChild(style)

if you want to add css file

var link = document.createElement('link')
link.setAttribute('rel', 'stylesheet')
link.setAttribute('type', 'text/css')
link.setAttribute('href', 'css/my.css')
document.getElementsByTagName('head')[0].appendChild(link)
share|improve this answer
    
+1 for non-CSS answer. –  Domenic Jul 3 '13 at 19:38
1  
+1 for instruction on adding in external CSS. –  guypursey Oct 7 '13 at 16:04
4  
JavaScript allows not to put ; at the end, but it can lead to unexpected behaviour and is considered bad practice. –  DanFromGermany Oct 16 '13 at 9:42

I have traditionally appended a <style> block when doing elements.

var style_rules = [];

style_rules.push("#" + myElemId + " { /* Rules */ } ");
/* ... */

var style = '<style type="text/css">' + style_rules.join("\n") + "</style>";
$("head").append(style);

An important thing to note is that because you don't know what any of the existing styles is, or what id's might conflict on the page, it's very useful to keep track of your id's inside your JavaScript application, then using those to populate the injected <style> block. I also tend to run my names through a prefix function to ensure that the generic names of wrapper, and unit do not conflict (they are turned into something like myunique_wrapper and myunique_unit.

Incorporating a basic CSS reset like #myWrapper {margin: 0; padding: 0} can be a decent starting platform for building your own custom styles.

Addressing your unique case, a live preview so to speak, I would designate a div with standard elements. Then when they click "update" read in the rules and append them to the head. If you want to negate any residual effects from past rules you can remove the last <style> element or better yet give your <style> element an id. I'm not sure if that kind of selection would work, but it should.

share|improve this answer
1  
I would recommend you adjust your answer to exclude the use of jQuery as the OP did not mention the use of it, nor did they tag the question with jquery –  jondavidjohn Jan 31 '11 at 3:46
    
@Jondavid: I make one call with jQuery, which is actually just as easily done with pure JavaScript. Simple DOM manipulation. My answer uses jQuery, if the OP would like to not use jQuery then he can of course modify that one small portion of my example code. –  Josh K Jan 31 '11 at 4:05
    
Then why wouldn't you just adjust your answer to conform to the OP's specifications? –  jondavidjohn Jan 31 '11 at 4:06
1  
@jondavid: The OP has neglected to give any specifications. –  Josh K Jan 31 '11 at 5:09
    
jquery is super common, and its not the responsibility of answers to try to guess at the tools at an OP's disposal. When in doubt, recommend what you would do. I certainly wouldn't recommend using pure dom manipulation.. –  B T Jul 5 '13 at 20:39

Can you add a style tag to the DOM, with the contents of the text-area in it? You may want to give it an id so you can change it later.

share|improve this answer

I recommend you start using a decent framework for your web/JavaScript development, personally I'd go with jQuery.

http://api.jquery.com/css/

There are some code snippets here that show you how to quickly set css properties for elements.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.