tags:

views:

57

answers:

5

How to add css styles inside <body> or <head>, placed in javascript file like this:

<style type="text/css">
    .popup-bg {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: #eee;
    }
</style>

The only way is to write css inside javascript, there is no access to html or any external css.

Thanks.

A: 
$('body').addClass('popup-bg');

Since there is guaranteed to be only one body element, selecting on the element itself works.

Stefan Kendall
there are much other styles, all of them must be written in javascript file and added with it to <body> or <head>
Happy
A: 

You can use something like $('body').addClass('popup-bg');

edl
can't use it, post is updated
Happy
+1  A: 

Use addClass method of jquery:

$('body').addClass('popup-bg');

Update:

  $(function(){
    $('head').append('
      <style type="text/css">
        .popup-bg {
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background: #eee;
        }
      </style>
    ');
  });
Sarfraz
can't use it, all styles must be written inside js-file
Happy
@Happy: We don't understand your question, please be more specific.
Sarfraz
From what I got, he wants to use JavaScript to add the styles into the document after it has been loaded...
animuson
@animuson, you are true
Happy
@Sarfraz Ahmed, I have updated the topic, now it should be more clear
Happy
@Happy: See my updated answer please.
Sarfraz
tryed to use it, doesn't work
Happy
seems that it deosn't work with multiline text.
Happy
@Sarfraz Javascript does not allow multi-line strings.
Pointy
+1  A: 
Pointy
this doesn't work with multiline text
Happy
Well, @Happy, that's because Javascript does not allow multi-line strings. You can put together a long string in Javascript by appending them together. If the strings have line separators in them, that will work fine (though I'm not sure what difference it makes).
Pointy
+1  A: 

IF the class is defined in your structure/markup with the class then:

$(".popup-bg").css({ position: fixed, 
        left: 0, 
        top: 0, 
        width: 100%, 
        height: 100%, 
        background: #eee 
 });

OR to add it to a particular tag:

    $("body").css({ position: fixed,  
            left: 0,  
            top: 0,  
            width: 100%,  
            height: 100%,  
            background: #eee  
     });

AND to inject it:

var style = document.createElement('style'); 

style.innerText = '.popup-bg{ position: fixed,  
            left: 0,  
            top: 0,  
            width: 100%,  
            height: 100%,  
            background: #eee  
     }'; 

document.head.appendChild(style); 
Mark Schultheiss
I think he wants to create new `<style>` blocks, not set the style on individual elements.
Pointy
@Pointy, you are true
Happy
OK well that last thing is what he wants, but be aware that if you try to set "innerHTML" on a `<style>` element in IE (maybe not IE8 but I haven't tried) you get an exception from the browser. You have to use "innerText" instead.
Pointy
so a form of my last edit, you could even add multiple text blocks (styles) to the "style" variable, then append it.
Mark Schultheiss
Yes, forgot the legacy IE error, edit for Text instead of HTML
Mark Schultheiss
Just an FYI, I believe you can clear the style with .empty(); for any styles aready on the page using jQuery $('style').empty(); IF you need to do that before adding the new style.
Mark Schultheiss
I don't understand, how to implement with innerText?
Happy
@Happy you tagged your question with the "jQuery" tag - if you're using jQuery you can just use the `.text()` call as in my answer. Also, `innerText` is not going to help you with your multi-line problem either, because you're still limited by what the programming language allows.
Pointy